2018-01-19 19:36:52 +03:00
|
|
|
// Package authn is in charge authenticating requests.
|
2018-01-26 12:55:53 +03:00
|
|
|
//
|
2018-01-26 11:26:21 +03:00
|
|
|
// Authenticators will be instantiated per identity provider URI.
|
|
|
|
// Currently only OpenID is supported.
|
|
|
|
//
|
|
|
|
// OpenID configuration and keys will be cached.
|
2018-01-19 19:36:52 +03:00
|
|
|
package authn
|
2017-10-03 17:27:04 +03:00
|
|
|
|
|
|
|
import (
|
2018-01-17 17:50:04 +03:00
|
|
|
"fmt"
|
2017-10-05 16:55:56 +03:00
|
|
|
"net/http"
|
2017-12-04 20:12:04 +03:00
|
|
|
"strings"
|
2017-10-03 17:27:04 +03:00
|
|
|
)
|
|
|
|
|
2018-01-26 11:26:21 +03:00
|
|
|
// UserInfo contains the necessary attributes used in Doorman policies.
|
|
|
|
type UserInfo struct {
|
|
|
|
ID string
|
|
|
|
Email string
|
|
|
|
Groups []string
|
2017-10-30 17:05:12 +03:00
|
|
|
}
|
|
|
|
|
2018-01-26 11:26:21 +03:00
|
|
|
// Authenticator is in charge of authenticating requests.
|
|
|
|
type Authenticator interface {
|
|
|
|
ValidateRequest(*http.Request) (*UserInfo, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var authenticators map[string]Authenticator
|
2017-10-03 17:27:04 +03:00
|
|
|
|
2017-11-17 17:15:19 +03:00
|
|
|
func init() {
|
2018-01-26 11:26:21 +03:00
|
|
|
authenticators = map[string]Authenticator{}
|
2017-11-17 17:15:19 +03:00
|
|
|
}
|
2017-11-06 14:57:43 +03:00
|
|
|
|
2018-01-26 11:26:21 +03:00
|
|
|
// NewAuthenticator instantiates or reuses an existing one for the specified
|
|
|
|
// identity provider.
|
|
|
|
func NewAuthenticator(idP string) (Authenticator, error) {
|
|
|
|
if !strings.HasPrefix(idP, "https://") {
|
|
|
|
return nil, fmt.Errorf("identify provider %q does not use the https:// scheme", idP)
|
2018-01-17 17:50:04 +03:00
|
|
|
}
|
2018-01-26 11:26:21 +03:00
|
|
|
// Reuse authenticator instances.
|
2018-01-29 15:02:10 +03:00
|
|
|
a, ok := authenticators[idP]
|
2017-11-17 17:15:19 +03:00
|
|
|
if !ok {
|
2018-01-26 11:26:21 +03:00
|
|
|
// Only OpenID is currently supported.
|
2018-01-29 15:02:10 +03:00
|
|
|
a = newOpenIDAuthenticator(idP)
|
|
|
|
authenticators[idP] = a
|
2017-10-03 17:27:04 +03:00
|
|
|
}
|
2018-01-29 15:02:10 +03:00
|
|
|
return a, nil
|
2017-10-03 17:27:04 +03:00
|
|
|
}
|