Documentation ¶
Index ¶
- Constants
- Variables
- func CompareRedirectURI(registered, provided string) error
- func GenerateRandomCode(n int) (string, error)
- func NewOAuth2Security(keysDir string, scheme *goa.OAuth2Security) chain.SecurityChainMiddleware
- func NewOAuth2SecurityMiddleware(resolver goaJwt.KeyResolver, scheme *goa.OAuth2Security) goa.Middleware
- type AuthProvider
- func (provider *AuthProvider) Authenticate(clientID, clientSecret string) error
- func (provider *AuthProvider) Authorize(clientID, scope, redirectURI string) (code string, err error)
- func (provider *AuthProvider) Exchange(clientID, code, redirectURI string) (refreshToken, accessToken string, expiresIn int, err error)
- func (provider *AuthProvider) Refresh(refreshToken, scope string) (newRefreshToken, accessToken string, expiresIn int, err error)
- type AuthToken
- type Client
- type ClientAuth
- type ClientService
- type TokenService
- type User
- type UserService
Constants ¶
const OAuth2SecurityType = "OAuth2"
OAuth2SecurityType is the name of the security type (JWT, OAUTH2, SAML...)
Variables ¶
var InternalServerError = goa.NewErrorClass("server_error", 500)
InternalServerError is a generic server error
var OAuth2AccessDenied = goa.NewErrorClass("access_denied", 403)
OAuth2AccessDenied is an access denied error for created auth
var OAuth2ErrorInvalidRedirectURI = goa.NewErrorClass("invalid_request", 400)
OAuth2ErrorInvalidRedirectURI is Bad Request error for invalid redirect URI
var OAuth2ErrorInvalidScope = goa.NewErrorClass("invalid_scope", 400)
OAuth2ErrorInvalidScope is Bad Request error for invalid scope requested
OAuth2ErrorUnauthorizedClient is an error for bad client credentials
Functions ¶
func CompareRedirectURI ¶
CompareRedirectURI compares the registered redirect URI with a provided one.
func GenerateRandomCode ¶
GenerateRandomCode generates a cryptographically strong random string with the specified length.
func NewOAuth2Security ¶
func NewOAuth2Security(keysDir string, scheme *goa.OAuth2Security) chain.SecurityChainMiddleware
NewOAuth2Security creates a OAuth2 SecurityChainMiddleware using a simple key resolver that loads the public keys from the keysDir. The key files must end in *.pub. The scheme is obtained from app/security.go.
func NewOAuth2SecurityMiddleware ¶
func NewOAuth2SecurityMiddleware(resolver goaJwt.KeyResolver, scheme *goa.OAuth2Security) goa.Middleware
NewOAuth2SecurityMiddleware creates a middleware that checks for the presence of an authorization header and validates its content. The steps taken by the middleware are: 1. Validate the "Bearer" token present in the "Authorization" header against the key(s) 2. If scopes are defined for the action validate them against the "scopes" JWT claim
Types ¶
type AuthProvider ¶
type AuthProvider struct { ClientService UserService TokenService tools.KeyStore SigningMethod string AuthCodeLength int RefreshTokenLength int AccessTokenValidityPeriod int ProviderName string }
AuthProvider holds the data for implementing the oauth2.Provider interface.
func (*AuthProvider) Authenticate ¶
func (provider *AuthProvider) Authenticate(clientID, clientSecret string) error
Authenticate checks the client credentials.
func (*AuthProvider) Authorize ¶
func (provider *AuthProvider) Authorize(clientID, scope, redirectURI string) (code string, err error)
Authorize performs the authorization of a client and generates basic ClientAuth.
type AuthToken ¶
type AuthToken struct { // AccessToken is the actual value of the access token. AccessToken string `json:"accessToken, omitempty" bson:"accessToken"` // RefreshToken holds the refresh token value. RefreshToken string `json:"refreshToken, omitempty" bson:"refreshToken"` // Unix timestamp of the time when the access token was issued. IssuedAt int64 `json:"issuedAt, omitempty" bson:"issuedAt"` // ValidFor is the time duration for which this token is valid. Expressed in milliseconds. ValidFor int `json:"validFor, omitempty" bson:"validFor"` // Scope is the scope for which this access token is valid. Scope string `json:"scope, omitempty" bson:"scope"` // ClientID is the reference to the client for which this token has been issued. ClientID string `json:"clientId, omitempty" bson:"clientId"` // UserID is the reference to the user for which this token has been issued. UserID string `json:"userId, omitempty" bson:"userId"` }
AuthToken holds the data for oauth2 token.
type Client ¶
type Client struct { ClientID string `json:"clientId, omitempty"` Name string `json:"name, omitempty"` Description string `json:"description, omitempty"` Website string `json:"domain, omitempty"` Secret string `json:"secret, omitempty"` }
Client holds the data for a specific client (app). A client must firt be registered for access on the platform.
type ClientAuth ¶
type ClientAuth struct { ClientID string `json:"clientId, omitempty" bson:"clientId"` UserID string `json:"userId, omitempty" bson:"userId"` Scope string `json:"scope, omitempty" bson:"scope"` Code string `json:"code, omitempty" bson:"code"` GeneratedAt int64 `json:"generatedAt, omitempty" bson:"generatedAt"` UserData string `json:"userData, omitempty" bson:"userData"` RedirectURI string `json:"redirectUri, omitempty" bson:"redirectUri"` Confirmed bool `json:"confirmed, omitempty" bson:"confirmed"` }
ClientAuth is an authorization record for a specific client (app) and user. It holds the data for a specific client that is (or needs to be) authorized by a user to access some part of the platform.
type ClientService ¶
type ClientService interface { // GetClient retrieves a Client by its ID. GetClient(clientID string) (*Client, error) // VerifyClientCredentials verfies that there is a registered Client with the specified client ID and client secret. // It returns the actual Client data if the credentials are valid, or nil if there is no such client. VerifyClientCredentials(clientID, clientSecret string) (*Client, error) // SaveClientAuth stores a ClientAuth. SaveClientAuth(clientAuth *ClientAuth) error // GetClientAuth retrieves a ClientAuth for the specified client ID and a generated random code for verification. GetClientAuth(clientID, code string) (*ClientAuth, error) // GetClientAuthForUser retrieves a ClientAuth for a Client and User. // Used when is situations where the access code is still not generated. GetClientAuthForUser(userID, clientID string) (*ClientAuth, error) // ConfirmClientAuth updates the Confirmed field (sets it to true). // Used to update the client auth once the user has accepted the client to access the data. ConfirmClientAuth(userID, clientID string) (*ClientAuth, error) // UpdateUserData updates the ClientAuth with the full user data. // This is techincally a workaround since the goa-oauth2 Provider does not take // into account the user in the access_grant flow. UpdateUserData(clientID, code, userID, userData string) error // DeleteClientAuth deletes the ClientAuth. // If you never call this, the ClientAuth should expire automatically after a certain period. DeleteClientAuth(clientID, code string) error }
ClientService is an interface that defines the access to a Client and ClientAuth.
type TokenService ¶
type TokenService interface { // SaveToken saves the token data to the backend. SaveToken(token AuthToken) error // GetToken retrieves the OAuth2Token for a refreshToken. GetToken(refreshToken string) (*AuthToken, error) // GetTokenForClient looks up an OAuth2Token for a specific client and user. // There should be only one such token. GetTokenForClient(userID, clientID string) (*AuthToken, error) }
TokenService defines the interface for managing OAuth2 Tokens.
type User ¶
type User struct { ID string `json:"id, omitempty"` Username string `json:"username, omitempty"` Email string `json:"email, omitempty"` Roles []string `json:"roles, omitempty"` Organizations []string `json:"organizations, omitempty"` Namespaces []string `json:"namespaces, omitempty"` ExternalID string `json:"externalId, omitempty"` Active bool `json:"active, omitempty"` }
User holds the user data.
type UserService ¶
type UserService interface { // VerifyUser verifies the credentials (username and password) and retrieves a // User if the credentials are valid. VerifyUser(username, password string) (*User, error) }
UserService defines an interface for verification of the user credentials. This is used in the access_grant flow, to login the user and then prompt it for confirmation about authorizing the client to access the services on the platform.