package
0.15.1
Repository: https://github.com/cisco-open/go-lanai.git
Documentation: pkg.go.dev

# README

OAuth2 Auth Server

List of Grants

Grant NameSuitable for Public Client
Client CredentialNo
PasswordNo
Authorization CodeYes
Refresh TokenYes
Switch TenantYes
Switch UserYes

Public clients are clients that are considered not able to keep a secret (such as javascript code executing in the browser.) The grants listed that are suitable for public clients relies on user authentication in addition to client authentication. You should consider the client authentication for public clients to be untrusted because the client cannot store secrets reliably.

Client Credential

This grant allows a client to authenticate itself using its clientId and client secret. The authorization server would return an access token upon authentication. The request can include an optional tenant id parameter to select the tenant for the resulting security context. If no tenant id is provided, the resulting tenancy for the security context would be based on the calculation for default tenant. If the client only have one assigned tenant, it will be used as the default. If the client have multiple assigned tenant, the security context will not have any tenancy. In that case, the caller must specify the tenant id to select a tenant if tenancy is desired.

Fields

FieldValueNote
MethodPOST
Target/v2/token
grant_typeclient_credentialsurl values
tenant_idtenant idoptional url values
Content-Typeapplication/x-www-form-urlencodedrequest header
Acceptapplication/jsonrequest header
AuthorizationUse the basic authclientID:Secret in base64

Curl Example

curl --location --request POST 'http://localhost:8900/auth/v2/token?grant_type=client_credentials' \
--header 'Authorization: Basic {base64_encode(clientId:clientSecret}'

Password

This grant allows client to authenticate both the client and the user by issuing both the client id client secret and username and password. The optional tenant id parameter will select the current tenant for the resulting security context. The tenants this authentication context can switch to is based on the intersection of the user's assigned tenants and the client's assigned tenants. This grant requires the authorization server to be able to authenticate user using its password. It's not applicable if the user is authenticated via an SSO protocol (such as SAML).

Fields

FieldValueNote
MethodPOST
Target/v2/token
grant_typepasswordurl values
usernameusernameurl values
passwordpasswordurl values
tenant_idtenant idoptional url values
Content-Typeapplication/x-www-form-urlencodedrequest header
Acceptapplication/jsonrequest header
AuthorizationUse the basic authclientID:Secret in base64

Curl Example

curl --location --request POST 'http://localhost:8900/auth/v2/token' \
--header 'Authorization: Basic {base64_encode(clientId:clientSecret}' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Accept: application/json' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username={username}' \
--data-urlencode 'password={password}' \
--data-urlencode 'tenant_id={tenant-id}'

Authorization Code

This grant allows client to authenticate both the client and user. Unlike the password grant, it doesn't need the user to provide their credentials to the client. The authorization request returns an auth code. The client needs to call the token API with the auth code to get the access token. In the token request, the tenant id parameter is an optional parameter to select the tenant for the resulting security context. The tenants this authentication context can switch to is based on the intersection of the user's assigned tenants and the client's assigned tenants.

Authorize Request Fields

See OAuth2 Spec for definition of corresponding fields

FieldValueNote
MethodGET
Target/v2/authorize
response_typecodeurl values
client_idclient idurl values
redirect_uriredirect uriurl values
statestateurl values

Example Request Issued from Browser

GET /auth/v2/authorize?response_type=code&client_id={client_id}}&redirect_uri={redirect_uri}&state={state} HTTP/1.1
Host: localhost:8900
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cookie: SESSION=785e280f-4b1e-490d-b447-581ee357ddeb
Upgrade-Insecure-Requests: 1
Pragma: no-cache
Cache-Control: no-cache

Token Request Fields

The response for this grant can also include a refresh token. See oauth2 spec on how to use the refresh token.

FieldValueNote
MethodPOST
Target/v2/token
grant_typeauthorization_codeurl values
codeusernameurl values
client_idclient_idurl values
redirect_uriredirect_uriurl values
tenant_idtenant idoptional url values
Content-Typeapplication/x-www-form-urlencodedrequest header
Acceptapplication/jsonrequest header
AuthorizationUse the basic authclientID:Secret in base64

Curl Example

curl --location --request POST 'http://localhost:8900/auth/v2/token' \
--header 'Authorization: Basic {base64_encode(clientId:clientSecret}' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Accept: application/json' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'code={code}' \
--data-urlencode 'client_id={client_id}' \
--data-urlencode 'redirect_uri={redirect_uri}'

Switch User

Use an access token to switch to a different user, resulting in a new access token. The current user must be granted the permission to switch user.

Fields

FieldValueNote
MethodPOST
Target/v2/token
grant_typeurn:cisco:nfv:oauth:grant-type:switch-userurl values
access_tokenaccess token valueurl values
switch_usernametarget user nameurl values
switch_user_idtarget user idurl values
tenant_idtenant idurl values
Content-Typeapplication/x-www-form-urlencodedrequest header
Acceptapplication/jsonrequest header
AuthorizationUse the basic authclientID:Secret in base64

Curl Example

curl --location --request POST 'http://localhost:8900/auth/v2/token' \
--header 'Authorization: Basic {base64_encode(clientId:clientSecret}' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Accept: application/json' \
--data-urlencode 'grant_type=urn:cisco:nfv:oauth:grant-type:switch-user' \
--data-urlencode 'access_token={access_token}' \
--data-urlencode `switch_user_id={switch_user_id}` \
--data-urlencode 'tenant_id={tenant-id}'

Switch Tenant

Use an access token to switch to a different tenant, resulting in a new access token. The current user must be granted the permission to switch tenant.

Fields

FieldValueNote
MethodPOST
Target/v2/token
grant_typeurn:cisco:nfv:oauth:grant-type:switch-tenanturl values
access_tokenaccess token valueurl values
tenant_idtenant idurl values
Content-Typeapplication/x-www-form-urlencodedrequest header
Acceptapplication/jsonrequest header
AuthorizationUse the basic authclientID:Secret in base64

Note that tenant external ID is deprecated. Please use tenantID for the field/value

Curl Example

curl --location --request POST 'http://localhost:8900/auth/v2/token' \
--header 'Authorization: Basic {base64_encode(clientId:clientSecret}' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Accept: application/json' \
--data-urlencode 'grant_type=urn:cisco:nfv:oauth:grant-type:switch-tenant' \
--data-urlencode 'access_token={access_token}' \
--data-urlencode 'tenant_id={tenant-id}'

Client Scopes

ScopeUsage
readnot used
writenot used
openidClient needs this scope to engage OIDC in addition to OAuth2
profileOIDC scope to get user profile related claims in user info and id token
emailOIDC scope to get user email related claims in user info and id token
addressOIDC scope to get user address related claims in user info and id token
phoneOIDC scope to get user phone related claims in user info and id token
token_detailsallows client to get token details from check_token API
tenant_hierarchyallows client to use the tenant_hierarchy API

Client Registration Consideration

Client registration should be implemented in by application. It is not implemented in the framework. It is the service implementation's responsibility to restrict grant types and scopes to clients as appropriate.

For grant types, client registration should require the client to have a client secret before allowing giving it a grant type that's not suitable for public clients. In addition, the switch tenant grant may not be suitable for a client that is supposed to work under only one tenant.

Grant NameSuitable for Public ClientSuitable for Self Registered Client
Client CredentialNoYes
PasswordNoYes
Authorization CodeYesYes
Refresh TokenYesYes
Switch TenantYesDepends on if client is supposed to be per tenant
Switch UserYesYes

For scopes, client registration should consider whether a scope is suitable to be given to a customer created client. The tenant_hierarchy and cross_tenant scope should not be given to customer registered client that is supposed to work only within the context of a single tenant.

The token_details scope should not be given to a customer registered client because it's related to the introspection API (the check_token API), and this API is meant to be used by resource owners. In most cases self registered clients are not resource owners.

Client registration should also consider whether a scope should be given to a public client. The token_details and tenant_hierarchy scope should not be given to any public client because they can't be trusted with keeping client secret.

ScopeSuitable for Public ClientSuitable for Self Registered Client
readYesYes
writeYesYes
openidYesYes
profileYesYes
emailYesYes
addressYesYes
phoneYesYes
token_detailsNoNo
tenant_hierarchyNoNo (assuming self registered client is isolated to tenant)

Check Token

This API allows a client to check a given token's validity. In addition, a client with the token_details scope can get the security context details represented by this token by specifying no_details=false

Fields

FieldValueNote
MethodPOST
Target/v2/check_token
tokentoken valueurl values
tenant_type_hintaccess_token or refresh_tokenurl values
Content-Typeapplication/x-www-form-urlencodedrequest header
Acceptapplication/jsonrequest header
AuthorizationUse the basic authclientID:Secret in base64

Curl Example

curl --location --request POST 'http://localhost:8900/auth/v2/check_token' \
--header 'Authorization: Basic {base64_encode(clientId:clientSecret}=' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'token={access_token}' \
--data-urlencode 'token_type_hint=access_token' \
--data-urlencode 'no_details=true'

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

ConvertToOAuthUserAuthentication takes any type of authentication and convert it into oauth2.Authentication.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
deja vu.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewWildcardUrlMatcher construct a wildcard URL matcher with given pattern The pattern should be escaped for URL endoding.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ValidateApproval approval param is a map with scope as keys and approval status as values.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
BasicClaimsTokenEnhancer impelments order.Ordered and TokenEnhancer.
No description provided by the author
No description provided by the author
CompositeTokenGranter implements TokenGranter.
No description provided by the author
No description provided by the author
DefaultAuthorizationService implements AuthorizationService.
DefaultAuthorizeHandler implements AuthorizeHandler it implement standard OAuth2 responses and keep a list of extensions for additional protocols such as OpenID Connect.
DefaultOAuth2Client implements security.Account & OAuth2Client.
DetailsTokenEnhancer implements order.Ordered and TokenEnhancer it populate token's additional metadata other than claims, issue/expiry time.
ExpiryTokenEnhancer implements order.Ordered and TokenEnhancer.
No description provided by the author
LegacyTokenEnhancer implements order.Ordered and TokenEnhancer LegacyTokenEnhancer add legacy claims and response fields that was supported by Java version of IDM but deprecated in Go version.
OAuth2ClientAccountStore wraps an delegate and implement both security.AccountStore and client oauth2.OAuth2ClientStore.
OAuth2ErrorHandler implements security.ErrorHandler It's responsible to handle all oauth2 errors.
RedisAuthorizationCodeStore store authorization code in Redis.
RefreshTokenEnhancer implements order.Ordered and TokenEnhancer RefreshTokenEnhancer is responsible to create refresh token and associate it with the given access token.
ResourceIdTokenEnhancer impelments order.Ordered and TokenEnhancer spring-security-oauth2 based java implementation expecting "aud" claims to be the resource ID.
StandardAuthorizeRequestProcessor implements ChainedAuthorizeRequestProcessor and order.Ordered it validate auth request against standard oauth2 specs.
No description provided by the author
No description provided by the author

# Interfaces

go:generate mockery --name AccessRevoker.
No description provided by the author
No description provided by the author
AuthorizationRegistry is responsible to keep track of refresh token and relationships between tokens, clients, users, sessions.
No description provided by the author
No description provided by the author
AuthorizeRequestProcessChain invoke index processor in the processing chain.
AuthorizeRequestProcessor validate and process incoming request AuthorizeRequestProcessor is the entry point interface for other components to use.
ChainedAuthorizeRequestProcessor validate and process incoming request and manually invoke index processor in the chain.
TokenEnhancer modify given oauth2.AccessToken or return a new token based on given context and auth Most TokenEnhancer responsible to add/modify claims of given access token But it's not limited to do so.
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
OverrideAuthOptions allows the oauth2.UserAuthOptions to be overridden during the conversion when creating and returning a new user authentication.
No description provided by the author
No description provided by the author
No description provided by the author