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"
|
|
|
|
|
2022-03-20 23:21:09 +08:00
|
|
|
"github.com/casdoor/casdoor/conf"
|
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"
|
|
|
|
)
|
|
|
|
|
2021-12-13 19:49:30 +08:00
|
|
|
func ParseSamlResponse(samlResponse string, providerType string) (string, error) {
|
2021-12-06 21:46:50 +08:00
|
|
|
samlResponse, _ = url.QueryUnescape(samlResponse)
|
2021-12-13 19:49:30 +08:00
|
|
|
sp, err := buildSp(&Provider{Type: providerType}, samlResponse)
|
2021-12-06 21:46:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
assertionInfo, err := sp.RetrieveAssertionInfo(samlResponse)
|
2023-03-23 21:38:33 +08:00
|
|
|
|
|
|
|
return assertionInfo.NameID, err
|
2021-12-06 21:46:50 +08:00
|
|
|
}
|
|
|
|
|
2023-03-23 21:38:33 +08:00
|
|
|
func GenerateSamlLoginUrl(id, relayState, lang string) (auth string, method string, err error) {
|
2021-12-06 21:46:50 +08:00
|
|
|
provider := GetProvider(id)
|
|
|
|
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
|
|
|
}
|
|
|
|
sp, err := buildSp(provider, "")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func buildSp(provider *Provider, samlResponse string) (*saml2.SAMLServiceProvider, error) {
|
2022-09-03 15:12:26 +08:00
|
|
|
origin := conf.GetConfigString("origin")
|
|
|
|
|
2021-12-06 21:46:50 +08:00
|
|
|
certStore := dsig.MemoryX509CertificateStore{
|
|
|
|
Roots: []*x509.Certificate{},
|
|
|
|
}
|
2022-09-03 15:12:26 +08:00
|
|
|
|
2021-12-06 21:46:50 +08:00
|
|
|
certEncodedData := ""
|
|
|
|
if samlResponse != "" {
|
2021-12-13 19:49:30 +08:00
|
|
|
certEncodedData = parseSamlResponse(samlResponse, provider.Type)
|
|
|
|
} else if provider.IdP != "" {
|
2021-12-06 21:46:50 +08:00
|
|
|
certEncodedData = provider.IdP
|
|
|
|
}
|
|
|
|
certData, err := base64.StdEncoding.DecodeString(certEncodedData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
idpCert, err := x509.ParseCertificate(certData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
certStore.Roots = append(certStore.Roots, idpCert)
|
|
|
|
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-06 21:46:50 +08:00
|
|
|
IDPCertificateStore: &certStore,
|
2021-12-15 21:38:00 +08:00
|
|
|
SignAuthnRequests: false,
|
|
|
|
SPKeyStore: dsig.RandomKeyStoreForTest(),
|
2021-12-06 21:46:50 +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
|
|
|
|
sp.SPKeyStore = buildSpKeyStore()
|
2021-12-06 21:46:50 +08:00
|
|
|
}
|
|
|
|
return sp, nil
|
|
|
|
}
|
2021-12-13 19:49:30 +08:00
|
|
|
|
|
|
|
func parseSamlResponse(samlResponse string, providerType string) string {
|
|
|
|
de, err := base64.StdEncoding.DecodeString(samlResponse)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
deStr := strings.Replace(string(de), "\n", "", -1)
|
|
|
|
tagMap := map[string]string{
|
|
|
|
"Aliyun IDaaS": "ds",
|
2021-12-15 21:38:00 +08:00
|
|
|
"Keycloak": "dsig",
|
2021-12-13 19:49:30 +08:00
|
|
|
}
|
|
|
|
tag := tagMap[providerType]
|
|
|
|
expression := fmt.Sprintf("<%s:X509Certificate>([\\s\\S]*?)</%s:X509Certificate>", tag, tag)
|
|
|
|
res := regexp.MustCompile(expression).FindStringSubmatch(deStr)
|
|
|
|
return res[1]
|
2021-12-15 21:38:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildSpKeyStore() dsig.X509KeyStore {
|
|
|
|
keyPair, err := tls.LoadX509KeyPair("object/token_jwt_key.pem", "object/token_jwt_key.key")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-02-13 23:39:27 +08:00
|
|
|
return &dsig.TLSCertKeyStore{
|
2021-12-15 21:38:00 +08:00
|
|
|
PrivateKey: keyPair.PrivateKey,
|
|
|
|
Certificate: keyPair.Certificate,
|
|
|
|
}
|
|
|
|
}
|