2015-08-18 05:57:27 +05:30
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/go-oidc/jose"
|
|
|
|
"github.com/coreos/go-oidc/key"
|
|
|
|
"github.com/coreos/go-oidc/oauth2"
|
|
|
|
"github.com/coreos/go-oidc/oidc"
|
|
|
|
"github.com/coreos/pkg/health"
|
2016-04-06 23:59:33 +05:30
|
|
|
"github.com/go-gorp/gorp"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/jonboulle/clockwork"
|
|
|
|
|
|
|
|
"github.com/coreos/dex/client"
|
2016-05-12 22:23:01 +05:30
|
|
|
clientmanager "github.com/coreos/dex/client/manager"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/coreos/dex/connector"
|
|
|
|
"github.com/coreos/dex/pkg/log"
|
|
|
|
"github.com/coreos/dex/refresh"
|
2016-06-10 04:26:37 +05:30
|
|
|
"github.com/coreos/dex/scope"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/coreos/dex/session"
|
2016-02-10 00:40:28 +05:30
|
|
|
sessionmanager "github.com/coreos/dex/session/manager"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/coreos/dex/user"
|
|
|
|
usersapi "github.com/coreos/dex/user/api"
|
|
|
|
useremail "github.com/coreos/dex/user/email"
|
2016-02-10 00:40:28 +05:30
|
|
|
usermanager "github.com/coreos/dex/user/manager"
|
2015-08-18 05:57:27 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
LoginPageTemplateName = "login.html"
|
|
|
|
RegisterTemplateName = "register.html"
|
|
|
|
VerifyEmailTemplateName = "verify-email.html"
|
|
|
|
SendResetPasswordEmailTemplateName = "send-reset-password.html"
|
|
|
|
ResetPasswordTemplateName = "reset-password.html"
|
|
|
|
|
|
|
|
APIVersion = "v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OIDCServer interface {
|
2016-06-18 00:25:21 +05:30
|
|
|
Client(string) (client.Client, error)
|
2015-08-29 04:33:51 +05:30
|
|
|
NewSession(connectorID, clientID, clientState string, redirectURL url.URL, nonce string, register bool, scope []string) (string, error)
|
2015-08-18 05:57:27 +05:30
|
|
|
Login(oidc.Identity, string) (string, error)
|
2016-06-01 00:28:12 +05:30
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
// CodeToken exchanges a code for an ID token and a refresh token string on success.
|
|
|
|
CodeToken(creds oidc.ClientCredentials, sessionKey string) (*jose.JWT, string, error)
|
2016-06-01 00:28:12 +05:30
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
ClientCredsToken(creds oidc.ClientCredentials) (*jose.JWT, error)
|
2016-06-01 00:28:12 +05:30
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
// RefreshToken takes a previously generated refresh token and returns a new ID token
|
|
|
|
// if the token is valid.
|
2016-06-10 04:26:37 +05:30
|
|
|
RefreshToken(creds oidc.ClientCredentials, scopes scope.Scopes, token string) (*jose.JWT, error)
|
2016-06-01 00:28:12 +05:30
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
KillSession(string) error
|
2016-06-01 00:28:12 +05:30
|
|
|
|
|
|
|
CrossClientAuthAllowed(requestingClientID, authorizingClientID string) (bool, error)
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
type JWTVerifierFactory func(clientID string) oidc.JWTVerifier
|
|
|
|
|
|
|
|
type Server struct {
|
2016-06-17 06:01:33 +05:30
|
|
|
IssuerURL url.URL
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
Templates *template.Template
|
|
|
|
LoginTemplate *template.Template
|
|
|
|
RegisterTemplate *template.Template
|
|
|
|
VerifyEmailTemplate *template.Template
|
|
|
|
SendResetPasswordEmailTemplate *template.Template
|
|
|
|
ResetPasswordTemplate *template.Template
|
2016-06-17 06:01:33 +05:30
|
|
|
|
|
|
|
HealthChecks []health.Checkable
|
|
|
|
Connectors []connector.Connector
|
|
|
|
|
|
|
|
ClientRepo client.ClientRepo
|
|
|
|
ConnectorConfigRepo connector.ConnectorConfigRepo
|
|
|
|
KeySetRepo key.PrivateKeySetRepo
|
|
|
|
RefreshTokenRepo refresh.RefreshTokenRepo
|
|
|
|
UserRepo user.UserRepo
|
|
|
|
PasswordInfoRepo user.PasswordInfoRepo
|
|
|
|
|
|
|
|
ClientManager *clientmanager.ClientManager
|
|
|
|
KeyManager key.PrivateKeyManager
|
|
|
|
SessionManager *sessionmanager.SessionManager
|
|
|
|
UserManager *usermanager.UserManager
|
|
|
|
|
|
|
|
UserEmailer *useremail.UserEmailer
|
|
|
|
|
|
|
|
EnableRegistration bool
|
|
|
|
EnableClientRegistration bool
|
|
|
|
RegisterOnFirstLogin bool
|
2015-08-18 05:57:27 +05:30
|
|
|
|
2016-04-06 23:59:33 +05:30
|
|
|
dbMap *gorp.DbMap
|
2015-08-18 05:57:27 +05:30
|
|
|
localConnectorID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Run() chan struct{} {
|
|
|
|
stop := make(chan struct{})
|
|
|
|
|
|
|
|
chans := []chan struct{}{
|
|
|
|
key.NewKeySetSyncer(s.KeySetRepo, s.KeyManager).Run(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, idpc := range s.Connectors {
|
|
|
|
chans = append(chans, idpc.Sync())
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-stop
|
|
|
|
for _, ch := range chans {
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return stop
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) KillSession(sessionKey string) error {
|
|
|
|
sessionID, err := s.SessionManager.ExchangeKey(sessionKey)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.SessionManager.Kill(sessionID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ProviderConfig() oidc.ProviderConfig {
|
2016-01-22 05:37:41 +05:30
|
|
|
authEndpoint := s.absURL(httpPathAuth)
|
|
|
|
tokenEndpoint := s.absURL(httpPathToken)
|
|
|
|
keysEndpoint := s.absURL(httpPathKeys)
|
2015-08-18 05:57:27 +05:30
|
|
|
cfg := oidc.ProviderConfig{
|
2016-01-22 05:37:41 +05:30
|
|
|
Issuer: &s.IssuerURL,
|
|
|
|
AuthEndpoint: &authEndpoint,
|
|
|
|
TokenEndpoint: &tokenEndpoint,
|
|
|
|
KeysEndpoint: &keysEndpoint,
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
GrantTypesSupported: []string{oauth2.GrantTypeAuthCode, oauth2.GrantTypeClientCreds},
|
|
|
|
ResponseTypesSupported: []string{"code"},
|
|
|
|
SubjectTypesSupported: []string{"public"},
|
2016-01-13 06:46:28 +05:30
|
|
|
IDTokenSigningAlgValues: []string{"RS256"},
|
2015-08-18 05:57:27 +05:30
|
|
|
TokenEndpointAuthMethodsSupported: []string{"client_secret_basic"},
|
|
|
|
}
|
|
|
|
|
2016-01-22 05:37:41 +05:30
|
|
|
if s.EnableClientRegistration {
|
|
|
|
regEndpoint := s.absURL(httpPathClientRegistration)
|
|
|
|
cfg.RegistrationEndpoint = ®Endpoint
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) absURL(paths ...string) url.URL {
|
|
|
|
url := s.IssuerURL
|
|
|
|
paths = append([]string{url.Path}, paths...)
|
|
|
|
url.Path = path.Join(paths...)
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) AddConnector(cfg connector.ConnectorConfig) error {
|
|
|
|
connectorID := cfg.ConnectorID()
|
|
|
|
ns := s.IssuerURL
|
|
|
|
ns.Path = path.Join(ns.Path, httpPathAuth, connectorID)
|
|
|
|
|
|
|
|
idpc, err := cfg.Connector(ns, s.Login, s.Templates)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Connectors = append(s.Connectors, idpc)
|
|
|
|
|
|
|
|
sortable := sortableIDPCs(s.Connectors)
|
|
|
|
sort.Sort(sortable)
|
|
|
|
|
|
|
|
// We handle the LocalConnector specially because it needs access to the
|
|
|
|
// UserRepo and the PasswordInfoRepo; if it turns out that other connectors
|
|
|
|
// need access to these resources we'll figure out how to provide it in a
|
|
|
|
// cleaner manner.
|
|
|
|
localConn, ok := idpc.(*connector.LocalConnector)
|
|
|
|
if ok {
|
|
|
|
s.localConnectorID = connectorID
|
|
|
|
if s.UserRepo == nil {
|
|
|
|
return errors.New("UserRepo cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.PasswordInfoRepo == nil {
|
|
|
|
return errors.New("PasswordInfoRepo cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
localConn.SetLocalIdentityProvider(&connector.LocalIdentityProvider{
|
|
|
|
UserRepo: s.UserRepo,
|
|
|
|
PasswordInfoRepo: s.PasswordInfoRepo,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Loaded IdP connector: id=%s type=%s", connectorID, cfg.ConnectorType())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) HTTPHandler() http.Handler {
|
|
|
|
checks := make([]health.Checkable, len(s.HealthChecks))
|
|
|
|
copy(checks, s.HealthChecks)
|
|
|
|
for _, idpc := range s.Connectors {
|
|
|
|
idpc := idpc
|
|
|
|
checks = append(checks, idpc)
|
|
|
|
}
|
|
|
|
|
|
|
|
clock := clockwork.NewRealClock()
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc(httpPathDiscovery, handleDiscoveryFunc(s.ProviderConfig()))
|
2015-10-01 05:05:58 +05:30
|
|
|
mux.HandleFunc(httpPathAuth, handleAuthFunc(s, s.Connectors, s.LoginTemplate, s.EnableRegistration))
|
2015-08-18 05:57:27 +05:30
|
|
|
mux.HandleFunc(httpPathToken, handleTokenFunc(s))
|
|
|
|
mux.HandleFunc(httpPathKeys, handleKeysFunc(s.KeyManager, clock))
|
|
|
|
mux.Handle(httpPathHealth, makeHealthHandler(checks))
|
2015-10-01 05:05:58 +05:30
|
|
|
|
|
|
|
if s.EnableRegistration {
|
2015-12-24 00:18:20 +05:30
|
|
|
mux.HandleFunc(httpPathRegister, handleRegisterFunc(s, s.RegisterTemplate))
|
2015-10-01 05:05:58 +05:30
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
mux.HandleFunc(httpPathEmailVerify, handleEmailVerifyFunc(s.VerifyEmailTemplate,
|
|
|
|
s.IssuerURL, s.KeyManager.PublicKeys, s.UserManager))
|
|
|
|
|
|
|
|
mux.Handle(httpPathVerifyEmailResend, s.NewClientTokenAuthHandler(handleVerifyEmailResendFunc(s.IssuerURL,
|
|
|
|
s.KeyManager.PublicKeys,
|
|
|
|
s.UserEmailer,
|
|
|
|
s.UserRepo,
|
2016-05-12 22:23:01 +05:30
|
|
|
s.ClientManager)))
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
mux.Handle(httpPathSendResetPassword, &SendResetPasswordEmailHandler{
|
|
|
|
tpl: s.SendResetPasswordEmailTemplate,
|
|
|
|
emailer: s.UserEmailer,
|
|
|
|
sm: s.SessionManager,
|
2016-05-12 22:23:01 +05:30
|
|
|
cm: s.ClientManager,
|
2015-08-18 05:57:27 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
mux.Handle(httpPathResetPassword, &ResetPasswordHandler{
|
|
|
|
tpl: s.ResetPasswordTemplate,
|
|
|
|
issuerURL: s.IssuerURL,
|
|
|
|
um: s.UserManager,
|
|
|
|
keysFunc: s.KeyManager.PublicKeys,
|
|
|
|
})
|
|
|
|
|
2015-11-10 04:05:11 +05:30
|
|
|
mux.Handle(httpPathAcceptInvitation, &InvitationHandler{
|
|
|
|
passwordResetURL: s.absURL(httpPathResetPassword),
|
|
|
|
issuerURL: s.IssuerURL,
|
|
|
|
um: s.UserManager,
|
|
|
|
keysFunc: s.KeyManager.PublicKeys,
|
|
|
|
signerFunc: s.KeyManager.Signer,
|
|
|
|
redirectValidityWindow: s.SessionManager.ValidityWindow,
|
|
|
|
})
|
|
|
|
|
2016-01-22 05:37:41 +05:30
|
|
|
if s.EnableClientRegistration {
|
|
|
|
mux.HandleFunc(httpPathClientRegistration, s.handleClientRegistration)
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
mux.HandleFunc(httpPathDebugVars, health.ExpvarHandler)
|
|
|
|
|
|
|
|
pcfg := s.ProviderConfig()
|
|
|
|
for _, idpc := range s.Connectors {
|
|
|
|
errorURL, err := url.Parse(fmt.Sprintf("%s?connector_id=%s", pcfg.AuthEndpoint, idpc.ID()))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
idpc.Register(mux, *errorURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
apiBasePath := path.Join(httpPathAPI, APIVersion)
|
|
|
|
registerDiscoveryResource(apiBasePath, mux)
|
|
|
|
|
2016-05-12 22:23:01 +05:30
|
|
|
usersAPI := usersapi.NewUsersAPI(s.UserManager, s.ClientManager, s.RefreshTokenRepo, s.UserEmailer, s.localConnectorID)
|
|
|
|
handler := NewUserMgmtServer(usersAPI, s.JWTVerifierFactory(), s.UserManager, s.ClientManager).HTTPHandler()
|
2015-09-29 02:21:48 +05:30
|
|
|
|
2016-04-06 23:59:33 +05:30
|
|
|
mux.Handle(apiBasePath+"/", handler)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
return http.Handler(mux)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClientTokenAuthHandler returns the given handler wrapped in middleware which requires a Client Bearer token.
|
|
|
|
func (s *Server) NewClientTokenAuthHandler(handler http.Handler) http.Handler {
|
|
|
|
return &clientTokenMiddleware{
|
|
|
|
issuerURL: s.IssuerURL.String(),
|
2016-05-12 22:23:01 +05:30
|
|
|
ciManager: s.ClientManager,
|
2015-08-18 05:57:27 +05:30
|
|
|
keysFunc: s.KeyManager.PublicKeys,
|
|
|
|
next: handler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-18 00:25:21 +05:30
|
|
|
func (s *Server) Client(clientID string) (client.Client, error) {
|
|
|
|
return s.ClientManager.Get(clientID)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2015-08-29 04:33:51 +05:30
|
|
|
func (s *Server) NewSession(ipdcID, clientID, clientState string, redirectURL url.URL, nonce string, register bool, scope []string) (string, error) {
|
|
|
|
sessionID, err := s.SessionManager.NewSession(ipdcID, clientID, clientState, redirectURL, nonce, register, scope)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Session %s created: clientID=%s clientState=%s", sessionID, clientID, clientState)
|
|
|
|
return s.SessionManager.NewSessionKey(sessionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Login(ident oidc.Identity, key string) (string, error) {
|
|
|
|
sessionID, err := s.SessionManager.ExchangeKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
ses, err := s.SessionManager.AttachRemoteIdentity(sessionID, ident)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
log.Infof("Session %s remote identity attached: clientID=%s identity=%#v", sessionID, ses.ClientID, ident)
|
|
|
|
|
|
|
|
if ses.Register {
|
|
|
|
code, err := s.SessionManager.NewSessionKey(sessionID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
ru := s.absURL(httpPathRegister)
|
|
|
|
q := ru.Query()
|
|
|
|
q.Set("code", code)
|
|
|
|
q.Set("state", ses.ClientState)
|
|
|
|
ru.RawQuery = q.Encode()
|
|
|
|
return ru.String(), nil
|
|
|
|
}
|
|
|
|
|
2016-06-17 06:01:33 +05:30
|
|
|
remoteIdentity := user.RemoteIdentity{ConnectorID: ses.ConnectorID, ID: ses.Identity.ID}
|
|
|
|
|
|
|
|
// Get the connector used to log the user in.
|
|
|
|
var conn connector.Connector
|
|
|
|
for _, c := range s.Connectors {
|
|
|
|
if c.ID() == ses.ConnectorID {
|
|
|
|
conn = c
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if conn == nil {
|
|
|
|
return "", fmt.Errorf("session contained invalid connector ID (%s)", ses.ConnectorID)
|
|
|
|
}
|
|
|
|
|
|
|
|
usr, err := s.UserRepo.GetByRemoteIdentity(nil, remoteIdentity)
|
2015-12-23 02:50:40 +05:30
|
|
|
if err == user.ErrorNotFound {
|
2016-06-17 06:01:33 +05:30
|
|
|
if ses.Identity.Email == "" {
|
|
|
|
// User doesn't have an existing account. Ask them to register.
|
|
|
|
u := newLoginURLFromSession(s.IssuerURL, ses, true, []string{ses.ConnectorID}, "register-maybe")
|
|
|
|
return u.String(), nil
|
|
|
|
}
|
|
|
|
|
2015-12-23 02:50:40 +05:30
|
|
|
// Does the user have an existing account with a different connector?
|
2016-06-17 06:01:33 +05:30
|
|
|
if connID, err := getConnectorForUserByEmail(s.UserRepo, ses.Identity.Email); err == nil {
|
|
|
|
// Ask user to sign in through existing account.
|
|
|
|
u := newLoginURLFromSession(s.IssuerURL, ses, false, []string{connID}, "wrong-connector")
|
|
|
|
return u.String(), nil
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2015-12-23 02:50:40 +05:30
|
|
|
|
2016-06-17 06:01:33 +05:30
|
|
|
// RegisterOnFirstLogin doesn't work for the local connector
|
|
|
|
tryToRegister := s.RegisterOnFirstLogin && (ses.ConnectorID != s.localConnectorID)
|
|
|
|
|
|
|
|
if !tryToRegister {
|
|
|
|
// User doesn't have an existing account. Ask them to register.
|
|
|
|
u := newLoginURLFromSession(s.IssuerURL, ses, true, []string{ses.ConnectorID}, "register-maybe")
|
|
|
|
return u.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// First time logging in through a remote connector. Attempt to register.
|
|
|
|
emailVerified := conn.TrustedEmailProvider()
|
|
|
|
usrID, err := s.UserManager.RegisterWithRemoteIdentity(ses.Identity.Email, emailVerified, remoteIdentity)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to register user: %v", err)
|
|
|
|
}
|
|
|
|
usr, err = s.UserManager.Get(usrID)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("getting created user: %v", err)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return "", fmt.Errorf("getting user: %v", err)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2015-09-26 02:54:51 +05:30
|
|
|
if usr.Disabled {
|
2016-06-17 06:01:33 +05:30
|
|
|
log.Errorf("user %s disabled", ses.Identity.Email)
|
2015-09-26 02:54:51 +05:30
|
|
|
return "", user.ErrorNotFound
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
ses, err = s.SessionManager.AttachUser(sessionID, usr.ID)
|
|
|
|
if err != nil {
|
2016-06-17 06:01:33 +05:30
|
|
|
return "", fmt.Errorf("attaching user to session: %v", err)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
log.Infof("Session %s user identified: clientID=%s user=%#v", sessionID, ses.ClientID, usr)
|
|
|
|
|
|
|
|
code, err := s.SessionManager.NewSessionKey(sessionID)
|
|
|
|
if err != nil {
|
2016-06-17 06:01:33 +05:30
|
|
|
return "", fmt.Errorf("creating new session key: %v", err)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
ru := ses.RedirectURL
|
|
|
|
q := ru.Query()
|
|
|
|
q.Set("code", code)
|
|
|
|
q.Set("state", ses.ClientState)
|
|
|
|
ru.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
return ru.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ClientCredsToken(creds oidc.ClientCredentials) (*jose.JWT, error) {
|
2016-05-12 22:23:01 +05:30
|
|
|
ok, err := s.ClientManager.Authenticate(creds)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
2016-05-12 22:23:01 +05:30
|
|
|
log.Errorf("Failed fetching client %s from manager: %v", creds.ID, err)
|
2015-08-18 05:57:27 +05:30
|
|
|
return nil, oauth2.NewError(oauth2.ErrorServerError)
|
2015-09-05 01:15:32 +05:30
|
|
|
}
|
|
|
|
if !ok {
|
2015-08-18 05:57:27 +05:30
|
|
|
return nil, oauth2.NewError(oauth2.ErrorInvalidClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
signer, err := s.KeyManager.Signer()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to generate ID token: %v", err)
|
|
|
|
return nil, oauth2.NewError(oauth2.ErrorServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
exp := now.Add(s.SessionManager.ValidityWindow)
|
|
|
|
claims := oidc.NewClaims(s.IssuerURL.String(), creds.ID, creds.ID, now, exp)
|
|
|
|
claims.Add("name", creds.ID)
|
|
|
|
|
|
|
|
jwt, err := jose.NewSignedJWT(claims, signer)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to generate ID token: %v", err)
|
|
|
|
return nil, oauth2.NewError(oauth2.ErrorServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Client token sent: clientID=%s", creds.ID)
|
|
|
|
|
|
|
|
return jwt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) CodeToken(creds oidc.ClientCredentials, sessionKey string) (*jose.JWT, string, error) {
|
2016-05-12 22:23:01 +05:30
|
|
|
ok, err := s.ClientManager.Authenticate(creds)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed fetching client %s from repo: %v", creds.ID, err)
|
|
|
|
return nil, "", oauth2.NewError(oauth2.ErrorServerError)
|
2015-09-05 01:15:32 +05:30
|
|
|
}
|
|
|
|
if !ok {
|
2015-08-18 05:57:27 +05:30
|
|
|
log.Errorf("Failed to Authenticate client %s", creds.ID)
|
|
|
|
return nil, "", oauth2.NewError(oauth2.ErrorInvalidClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionID, err := s.SessionManager.ExchangeKey(sessionKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", oauth2.NewError(oauth2.ErrorInvalidGrant)
|
|
|
|
}
|
|
|
|
|
|
|
|
ses, err := s.SessionManager.Kill(sessionID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", oauth2.NewError(oauth2.ErrorInvalidRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ses.ClientID != creds.ID {
|
|
|
|
return nil, "", oauth2.NewError(oauth2.ErrorInvalidGrant)
|
|
|
|
}
|
|
|
|
|
|
|
|
signer, err := s.KeyManager.Signer()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to generate ID token: %v", err)
|
|
|
|
return nil, "", oauth2.NewError(oauth2.ErrorServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := s.UserRepo.Get(nil, ses.UserID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to fetch user %q from repo: %v: ", ses.UserID, err)
|
|
|
|
return nil, "", oauth2.NewError(oauth2.ErrorServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
claims := ses.Claims(s.IssuerURL.String())
|
|
|
|
user.AddToClaims(claims)
|
|
|
|
|
2016-06-11 04:17:40 +05:30
|
|
|
s.addClaimsFromScope(claims, ses.Scope, ses.ClientID)
|
2016-06-03 01:35:18 +05:30
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
jwt, err := jose.NewSignedJWT(claims, signer)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to generate ID token: %v", err)
|
|
|
|
return nil, "", oauth2.NewError(oauth2.ErrorServerError)
|
|
|
|
}
|
|
|
|
|
2015-09-01 02:08:06 +05:30
|
|
|
// Generate refresh token when 'scope' contains 'offline_access'.
|
|
|
|
var refreshToken string
|
|
|
|
|
|
|
|
for _, scope := range ses.Scope {
|
|
|
|
if scope == "offline_access" {
|
|
|
|
log.Infof("Session %s requests offline access, will generate refresh token", sessionID)
|
|
|
|
|
2016-06-10 04:26:37 +05:30
|
|
|
refreshToken, err = s.RefreshTokenRepo.Create(ses.UserID, creds.ID, ses.Scope)
|
2015-09-01 02:08:06 +05:30
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
log.Errorf("Failed to generate refresh token: %v", err)
|
|
|
|
return nil, "", oauth2.NewError(oauth2.ErrorServerError)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2015-09-01 02:08:06 +05:30
|
|
|
|
|
|
|
log.Infof("Session %s token sent: clientID=%s", sessionID, creds.ID)
|
2015-08-18 05:57:27 +05:30
|
|
|
return jwt, refreshToken, nil
|
|
|
|
}
|
|
|
|
|
2016-06-10 04:26:37 +05:30
|
|
|
func (s *Server) RefreshToken(creds oidc.ClientCredentials, scopes scope.Scopes, token string) (*jose.JWT, error) {
|
2016-05-12 22:23:01 +05:30
|
|
|
ok, err := s.ClientManager.Authenticate(creds)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed fetching client %s from repo: %v", creds.ID, err)
|
|
|
|
return nil, oauth2.NewError(oauth2.ErrorServerError)
|
2015-09-05 01:15:32 +05:30
|
|
|
}
|
|
|
|
if !ok {
|
2015-08-18 05:57:27 +05:30
|
|
|
log.Errorf("Failed to Authenticate client %s", creds.ID)
|
|
|
|
return nil, oauth2.NewError(oauth2.ErrorInvalidClient)
|
|
|
|
}
|
|
|
|
|
2016-06-10 04:26:37 +05:30
|
|
|
userID, rtScopes, err := s.RefreshTokenRepo.Verify(creds.ID, token)
|
2015-08-18 05:57:27 +05:30
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
case refresh.ErrorInvalidToken:
|
|
|
|
return nil, oauth2.NewError(oauth2.ErrorInvalidRequest)
|
|
|
|
case refresh.ErrorInvalidClientID:
|
|
|
|
return nil, oauth2.NewError(oauth2.ErrorInvalidClient)
|
|
|
|
default:
|
|
|
|
return nil, oauth2.NewError(oauth2.ErrorServerError)
|
2016-06-10 04:26:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if len(scopes) == 0 {
|
|
|
|
scopes = rtScopes
|
|
|
|
} else {
|
|
|
|
if !rtScopes.Contains(scopes) {
|
|
|
|
return nil, oauth2.NewError(oauth2.ErrorInvalidRequest)
|
|
|
|
}
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
user, err := s.UserRepo.Get(nil, userID)
|
|
|
|
if err != nil {
|
|
|
|
// The error can be user.ErrorNotFound, but we are not deleting
|
|
|
|
// user at this moment, so this shouldn't happen.
|
|
|
|
log.Errorf("Failed to fetch user %q from repo: %v: ", userID, err)
|
|
|
|
return nil, oauth2.NewError(oauth2.ErrorServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
signer, err := s.KeyManager.Signer()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to refresh ID token: %v", err)
|
|
|
|
return nil, oauth2.NewError(oauth2.ErrorServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
expireAt := now.Add(session.DefaultSessionValidityWindow)
|
|
|
|
|
|
|
|
claims := oidc.NewClaims(s.IssuerURL.String(), user.ID, creds.ID, now, expireAt)
|
|
|
|
user.AddToClaims(claims)
|
|
|
|
|
2016-06-11 04:17:40 +05:30
|
|
|
s.addClaimsFromScope(claims, scope.Scopes(scopes), creds.ID)
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
jwt, err := jose.NewSignedJWT(claims, signer)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to generate ID token: %v", err)
|
|
|
|
return nil, oauth2.NewError(oauth2.ErrorServerError)
|
|
|
|
}
|
|
|
|
|
2015-09-01 02:08:06 +05:30
|
|
|
log.Infof("New token sent: clientID=%s", creds.ID)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
return jwt, nil
|
|
|
|
}
|
|
|
|
|
2016-06-01 00:28:12 +05:30
|
|
|
func (s *Server) CrossClientAuthAllowed(requestingClientID, authorizingClientID string) (bool, error) {
|
2016-06-03 01:35:18 +05:30
|
|
|
alloweds, err := s.ClientRepo.GetTrustedPeers(nil, authorizingClientID)
|
2016-06-01 00:28:12 +05:30
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, allowed := range alloweds {
|
|
|
|
if requestingClientID == allowed {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
func (s *Server) JWTVerifierFactory() JWTVerifierFactory {
|
|
|
|
noop := func() error { return nil }
|
|
|
|
|
|
|
|
keyFunc := func() []key.PublicKey {
|
|
|
|
keys, err := s.KeyManager.PublicKeys()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error getting public keys from manager: %v", err)
|
|
|
|
return []key.PublicKey{}
|
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
return func(clientID string) oidc.JWTVerifier {
|
|
|
|
|
|
|
|
return oidc.NewJWTVerifier(s.IssuerURL.String(), clientID, noop, keyFunc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-11 04:17:40 +05:30
|
|
|
// addClaimsFromScope adds claims that are based on the scopes that the client requested.
|
|
|
|
// Currently, these include cross-client claims (aud, azp).
|
|
|
|
func (s *Server) addClaimsFromScope(claims jose.Claims, scopes scope.Scopes, clientID string) error {
|
|
|
|
crossClientIDs := scopes.CrossClientIDs()
|
|
|
|
if len(crossClientIDs) > 0 {
|
|
|
|
var aud []string
|
|
|
|
for _, id := range crossClientIDs {
|
|
|
|
if clientID == id {
|
|
|
|
aud = append(aud, id)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
allowed, err := s.CrossClientAuthAllowed(clientID, id)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to check cross client auth. reqClientID %v; authClient:ID %v; err: %v", clientID, id, err)
|
|
|
|
return oauth2.NewError(oauth2.ErrorServerError)
|
|
|
|
}
|
|
|
|
if !allowed {
|
|
|
|
err := oauth2.NewError(oauth2.ErrorInvalidRequest)
|
|
|
|
err.Description = fmt.Sprintf(
|
|
|
|
"%q is not authorized to perform cross-client requests for %q",
|
|
|
|
clientID, id)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
aud = append(aud, id)
|
|
|
|
}
|
|
|
|
if len(aud) == 1 {
|
|
|
|
claims.Add("aud", aud[0])
|
|
|
|
} else {
|
|
|
|
claims.Add("aud", aud)
|
|
|
|
}
|
|
|
|
claims.Add("azp", clientID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
type sortableIDPCs []connector.Connector
|
|
|
|
|
|
|
|
func (s sortableIDPCs) Len() int {
|
|
|
|
return len([]connector.Connector(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sortableIDPCs) Less(i, j int) bool {
|
|
|
|
idpcs := []connector.Connector(s)
|
|
|
|
return idpcs[i].ID() < idpcs[j].ID()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sortableIDPCs) Swap(i, j int) {
|
|
|
|
idpcs := []connector.Connector(s)
|
|
|
|
idpcs[i], idpcs[j] = idpcs[j], idpcs[i]
|
|
|
|
}
|