2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-12-06 21:46:50 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2021-12-15 21:38:00 +08:00
|
|
|
"crypto/tls"
|
2021-12-06 21:46:50 +08:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2023-10-20 21:11:36 +08:00
|
|
|
"github.com/casdoor/casdoor/idp"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
|
2022-10-23 15:16:24 +08:00
|
|
|
"github.com/casdoor/casdoor/i18n"
|
2021-12-06 21:46:50 +08:00
|
|
|
saml2 "github.com/russellhaering/gosaml2"
|
|
|
|
dsig "github.com/russellhaering/goxmldsig"
|
|
|
|
)
|
|
|
|
|
2023-10-20 21:11:36 +08:00
|
|
|
func ParseSamlResponse(samlResponse string, provider *Provider, host string) (*idp.UserInfo, error) {
|
2021-12-06 21:46:50 +08:00
|
|
|
samlResponse, _ = url.QueryUnescape(samlResponse)
|
2023-04-16 00:36:25 +08:00
|
|
|
sp, err := buildSp(provider, samlResponse, host)
|
2021-12-06 21:46:50 +08:00
|
|
|
if err != nil {
|
2023-10-20 21:11:36 +08:00
|
|
|
return nil, err
|
2021-12-06 21:46:50 +08:00
|
|
|
}
|
2023-03-23 21:38:33 +08:00
|
|
|
|
2023-04-16 00:36:25 +08:00
|
|
|
assertionInfo, err := sp.RetrieveAssertionInfo(samlResponse)
|
|
|
|
if err != nil {
|
2023-10-20 21:11:36 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
userInfoMap := make(map[string]string)
|
|
|
|
for spAttr, idpAttr := range provider.UserMapping {
|
|
|
|
for _, attr := range assertionInfo.Values {
|
|
|
|
if attr.Name == idpAttr {
|
|
|
|
userInfoMap[spAttr] = attr.Values[0].Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
userInfoMap["id"] = assertionInfo.NameID
|
|
|
|
|
|
|
|
customUserInfo := &idp.CustomUserInfo{}
|
|
|
|
err = mapstructure.Decode(userInfoMap, customUserInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-04-16 00:36:25 +08:00
|
|
|
}
|
2023-10-20 21:11:36 +08:00
|
|
|
userInfo := &idp.UserInfo{
|
|
|
|
Id: customUserInfo.Id,
|
|
|
|
Username: customUserInfo.Username,
|
|
|
|
DisplayName: customUserInfo.DisplayName,
|
|
|
|
Email: customUserInfo.Email,
|
|
|
|
AvatarUrl: customUserInfo.AvatarUrl,
|
|
|
|
}
|
|
|
|
return userInfo, err
|
2021-12-06 21:46:50 +08:00
|
|
|
}
|
|
|
|
|
2023-04-16 00:36:25 +08:00
|
|
|
func GenerateSamlRequest(id, relayState, host, lang string) (auth string, method string, err error) {
|
2023-05-30 15:49:39 +08:00
|
|
|
provider, err := GetProvider(id)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2021-12-06 21:46:50 +08:00
|
|
|
if provider.Category != "SAML" {
|
2022-12-07 13:13:23 +08:00
|
|
|
return "", "", fmt.Errorf(i18n.Translate(lang, "saml_sp:provider %s's category is not SAML"), provider.Name)
|
2021-12-06 21:46:50 +08:00
|
|
|
}
|
2023-04-16 00:36:25 +08:00
|
|
|
|
|
|
|
sp, err := buildSp(provider, "", host)
|
2021-12-06 21:46:50 +08:00
|
|
|
if err != nil {
|
2021-12-15 21:38:00 +08:00
|
|
|
return "", "", err
|
2021-12-06 21:46:50 +08:00
|
|
|
}
|
2023-03-23 21:38:33 +08:00
|
|
|
|
2021-12-15 21:38:00 +08:00
|
|
|
if provider.EnableSignAuthnRequest {
|
|
|
|
post, err := sp.BuildAuthBodyPost(relayState)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
auth = string(post[:])
|
|
|
|
method = "POST"
|
|
|
|
} else {
|
|
|
|
auth, err = sp.BuildAuthURL(relayState)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
method = "GET"
|
2021-12-06 21:46:50 +08:00
|
|
|
}
|
2021-12-15 21:38:00 +08:00
|
|
|
return auth, method, nil
|
2021-12-06 21:46:50 +08:00
|
|
|
}
|
|
|
|
|
2023-04-16 00:36:25 +08:00
|
|
|
func buildSp(provider *Provider, samlResponse string, host string) (*saml2.SAMLServiceProvider, error) {
|
|
|
|
_, origin := getOriginFromHost(host)
|
2022-09-03 15:12:26 +08:00
|
|
|
|
2023-04-16 00:36:25 +08:00
|
|
|
certStore, err := buildSpCertificateStore(provider, samlResponse)
|
2021-12-06 21:46:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-16 00:36:25 +08:00
|
|
|
|
2021-12-06 21:46:50 +08:00
|
|
|
sp := &saml2.SAMLServiceProvider{
|
2021-12-12 19:26:06 +08:00
|
|
|
ServiceProviderIssuer: fmt.Sprintf("%s/api/acs", origin),
|
|
|
|
AssertionConsumerServiceURL: fmt.Sprintf("%s/api/acs", origin),
|
2021-12-15 21:38:00 +08:00
|
|
|
SignAuthnRequests: false,
|
2023-04-16 00:36:25 +08:00
|
|
|
IDPCertificateStore: &certStore,
|
2021-12-15 21:38:00 +08:00
|
|
|
SPKeyStore: dsig.RandomKeyStoreForTest(),
|
2021-12-06 21:46:50 +08:00
|
|
|
}
|
2023-04-16 00:36:25 +08:00
|
|
|
|
2021-12-13 19:49:30 +08:00
|
|
|
if provider.Endpoint != "" {
|
2021-12-06 21:46:50 +08:00
|
|
|
sp.IdentityProviderSSOURL = provider.Endpoint
|
|
|
|
sp.IdentityProviderIssuer = provider.IssuerUrl
|
2021-12-15 21:38:00 +08:00
|
|
|
}
|
|
|
|
if provider.EnableSignAuthnRequest {
|
|
|
|
sp.SignAuthnRequests = true
|
2023-05-30 15:49:39 +08:00
|
|
|
sp.SPKeyStore, err = buildSpKeyStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-06 21:46:50 +08:00
|
|
|
}
|
2023-04-16 00:36:25 +08:00
|
|
|
|
2021-12-06 21:46:50 +08:00
|
|
|
return sp, nil
|
|
|
|
}
|
2021-12-13 19:49:30 +08:00
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func buildSpKeyStore() (dsig.X509KeyStore, error) {
|
2023-04-16 00:36:25 +08:00
|
|
|
keyPair, err := tls.LoadX509KeyPair("object/token_jwt_key.pem", "object/token_jwt_key.key")
|
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return nil, err
|
2023-04-16 00:36:25 +08:00
|
|
|
}
|
|
|
|
return &dsig.TLSCertKeyStore{
|
|
|
|
PrivateKey: keyPair.PrivateKey,
|
|
|
|
Certificate: keyPair.Certificate,
|
2023-05-30 15:49:39 +08:00
|
|
|
}, nil
|
2023-04-16 00:36:25 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func buildSpCertificateStore(provider *Provider, samlResponse string) (certStore dsig.MemoryX509CertificateStore, err error) {
|
2023-04-16 00:36:25 +08:00
|
|
|
certEncodedData := ""
|
|
|
|
if samlResponse != "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
certEncodedData, err = getCertificateFromSamlResponse(samlResponse, provider.Type)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-04-16 00:36:25 +08:00
|
|
|
} else if provider.IdP != "" {
|
|
|
|
certEncodedData = provider.IdP
|
|
|
|
}
|
|
|
|
|
|
|
|
certData, err := base64.StdEncoding.DecodeString(certEncodedData)
|
|
|
|
if err != nil {
|
|
|
|
return dsig.MemoryX509CertificateStore{}, err
|
|
|
|
}
|
|
|
|
idpCert, err := x509.ParseCertificate(certData)
|
|
|
|
if err != nil {
|
|
|
|
return dsig.MemoryX509CertificateStore{}, err
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
certStore = dsig.MemoryX509CertificateStore{
|
2023-04-16 00:36:25 +08:00
|
|
|
Roots: []*x509.Certificate{idpCert},
|
|
|
|
}
|
|
|
|
return certStore, nil
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
func getCertificateFromSamlResponse(samlResponse string, providerType string) (string, error) {
|
2021-12-13 19:49:30 +08:00
|
|
|
de, err := base64.StdEncoding.DecodeString(samlResponse)
|
|
|
|
if err != nil {
|
2023-05-30 15:49:39 +08:00
|
|
|
return "", err
|
2021-12-13 19:49:30 +08:00
|
|
|
}
|
2023-10-20 21:11:36 +08:00
|
|
|
var (
|
|
|
|
expression string
|
|
|
|
deStr = strings.Replace(string(de), "\n", "", -1)
|
|
|
|
tagMap = map[string]string{
|
|
|
|
"Aliyun IDaaS": "ds",
|
|
|
|
"Keycloak": "dsig",
|
|
|
|
}
|
|
|
|
)
|
2021-12-13 19:49:30 +08:00
|
|
|
tag := tagMap[providerType]
|
2023-10-20 21:11:36 +08:00
|
|
|
if tag == "" {
|
|
|
|
// <ds:X509Certificate>...</ds:X509Certificate>
|
|
|
|
// <dsig:X509Certificate>...</dsig:X509Certificate>
|
|
|
|
// <X509Certificate>...</X509Certificate>
|
|
|
|
// ...
|
|
|
|
expression = "<[^>]*:?X509Certificate>([\\s\\S]*?)<[^>]*:?X509Certificate>"
|
|
|
|
} else {
|
|
|
|
expression = fmt.Sprintf("<%s:X509Certificate>([\\s\\S]*?)</%s:X509Certificate>", tag, tag)
|
|
|
|
}
|
2021-12-13 19:49:30 +08:00
|
|
|
res := regexp.MustCompile(expression).FindStringSubmatch(deStr)
|
2023-05-30 15:49:39 +08:00
|
|
|
return res[1], nil
|
2021-12-15 21:38:00 +08:00
|
|
|
}
|