Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
package external
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/cortezaproject/corteza/server/auth/saml"
|
|
"github.com/cortezaproject/corteza/server/system/types"
|
|
"github.com/crewjam/saml/samlsp"
|
|
"github.com/markbates/goth/gothic"
|
|
)
|
|
|
|
type (
|
|
ExternalAuthHandler interface {
|
|
BeginUserAuth(http.ResponseWriter, *http.Request)
|
|
CompleteUserAuth(http.ResponseWriter, *http.Request) (u *types.ExternalAuthUser, err error)
|
|
}
|
|
|
|
externalSamlAuthHandler struct {
|
|
service *saml.SamlSPService
|
|
}
|
|
|
|
externalDefaultAuthHandler struct{}
|
|
)
|
|
|
|
func (eh *externalDefaultAuthHandler) BeginUserAuth(w http.ResponseWriter, r *http.Request) {
|
|
gothic.BeginAuthHandler(w, r)
|
|
}
|
|
|
|
func (eh *externalDefaultAuthHandler) CompleteUserAuth(w http.ResponseWriter, r *http.Request) (u *types.ExternalAuthUser, err error) {
|
|
gu, err := gothic.CompleteUserAuth(w, r)
|
|
u = &types.ExternalAuthUser{gu}
|
|
|
|
return
|
|
}
|
|
|
|
func (eh *externalSamlAuthHandler) BeginUserAuth(w http.ResponseWriter, r *http.Request) {
|
|
_, err := eh.service.Handler().Session.GetSession(r)
|
|
|
|
if err == samlsp.ErrNoSession {
|
|
eh.service.Handler().HandleStartAuthFlow(w, r)
|
|
}
|
|
}
|
|
|
|
func (eh *externalSamlAuthHandler) CompleteUserAuth(w http.ResponseWriter, r *http.Request) (u *types.ExternalAuthUser, err error) {
|
|
var (
|
|
session samlsp.Session
|
|
)
|
|
|
|
if session, err = eh.service.Handler().Session.GetSession(r); err != nil {
|
|
return
|
|
}
|
|
|
|
if session != nil {
|
|
sess := session.(samlsp.JWTSessionClaims)
|
|
|
|
if sess.StandardClaims.Valid() != nil {
|
|
return nil, sess.StandardClaims.Valid()
|
|
}
|
|
|
|
u = &types.ExternalAuthUser{}
|
|
u.Provider = "saml"
|
|
|
|
// get identifier for use with Corteza (email)
|
|
u.Email = eh.service.GuessIdentifier(sess.Attributes)
|
|
|
|
// try to get email from jwt claims
|
|
if u.Email == "" {
|
|
u.Email = sess.StandardClaims.Subject
|
|
u.Name = sess.StandardClaims.Subject
|
|
u.NickName = sess.StandardClaims.Subject
|
|
} else {
|
|
u.Name = sess.Attributes.Get(eh.service.IDPUserMeta.Name)
|
|
u.NickName = sess.Attributes.Get(eh.service.IDPUserMeta.Handle)
|
|
}
|
|
|
|
u.UserID = sess.Attributes.Get("SessionIndex")
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func NewSamlExternalHandler(s *saml.SamlSPService) *externalSamlAuthHandler {
|
|
return &externalSamlAuthHandler{
|
|
service: s,
|
|
}
|
|
}
|
|
|
|
func NewDefaultExternalHandler() *externalDefaultAuthHandler {
|
|
return &externalDefaultAuthHandler{}
|
|
}
|