auth

package
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2019 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrAuthContext    errors.ErrorCode = "AUTH_CONTEXT_SETUP_FAILED"
	ErrConfigFileRead errors.ErrorCode = "CONFIG_OPTION_FILE_READ_FAILED"
)
View Source
const (
	ErrSecureCookie errors.ErrorCode = "SECURE_COOKIE_ERROR"
	// #nosec
	ErrInvalidCsrfToken errors.ErrorCode = "CSRF_TOKEN_VALIDATION_FAILED"
)
View Source
const (
	ErrB64Decoding errors.ErrorCode = "BINARY_DECODING_FAILED"
	// #nosec
	ErrTokenNil errors.ErrorCode = "EMPTY_OAUTH_TOKEN"
)
View Source
const (
	ErrRefreshingToken errors.ErrorCode = "TOKEN_REFRESH_FAILURE"
	ErrTokenExpired    errors.ErrorCode = "JWT_EXPIRED"
	ErrJwtValidation   errors.ErrorCode = "JWT_VERIFICATION_FAILED"
)
View Source
const AuthorizationResponseCodeType = "code"
View Source
const BearerScheme = "Bearer"
View Source
const CsrfFormKey = "state"

OAuth2 Parameters

View Source
const DefaultAuthorizationHeader = "authorization"
View Source
const (
	ErrIdpClient errors.ErrorCode = "IDP_REQUEST_FAILED"
)
View Source
const (
	IdpConnectionTimeout = 10 * time.Second
)
View Source
const (
	LoginRedirectURLParameter = "redirect_url"
)
View Source
const MetadataEndpoint = ".well-known/oauth-authorization-server"

https://tools.ietf.org/html/rfc8414 This should be defined without a leading slash. If there is one, the url library's ResolveReference will make it a root path

View Source
const OfflineAccessType = "offline_access"

IDP specific

View Source
const OidcScope = "openid"
View Source
const ProfileScope = "profile"
View Source
const RefreshToken = "refresh_token"

Variables

View Source
var AllowedChars = []rune("abcdefghijklmnopqrstuvwxyz1234567890")

Functions

func AuthenticationLoggingInterceptor

func AuthenticationLoggingInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)

func GetAuthenticationCustomMetadataInterceptor

func GetAuthenticationCustomMetadataInterceptor(authCtx interfaces.AuthenticationContext) grpc.UnaryServerInterceptor

This function produces a gRPC middleware interceptor intended to be used when running authentication with non-default gRPC headers (metadata). Because the default `authorization` header is reserved for use by Envoy, clients wishing to pass tokens to Admin will need to use a different string, specified in this package's Config object. This interceptor will scan for that arbitrary string, and then rename it to the default string, which the downstream auth/auditing interceptors will detect and validate.

func GetAuthenticationInterceptor

func GetAuthenticationInterceptor(authContext interfaces.AuthenticationContext) func(context.Context) (context.Context, error)

This function will only look for a token from the request metadata, verify it, and extract the user email if valid. Unless there is an error, it will not return an unauthorized status. That is up to subsequent functions to decide, based on configuration. We don't want to require authentication for all endpoints.

func GetCallbackHandler

func GetCallbackHandler(ctx context.Context, authContext interfaces.AuthenticationContext) http.HandlerFunc

func GetLoginHandler

func GetLoginHandler(ctx context.Context, authContext interfaces.AuthenticationContext) http.HandlerFunc

func GetMeEndpointHandler

func GetMeEndpointHandler(ctx context.Context, authCtx interfaces.AuthenticationContext) http.HandlerFunc

TODO: Add this to the Admin service IDL in Flyte IDL so that this can be exposed from gRPC as well. This returns a handler that will retrieve user info, from the OAuth2 authorization server. See the OpenID Connect spec at https://openid.net/specs/openid-connect-core-1_0.html#UserInfoResponse for more information.

func GetMetadataEndpointRedirectHandler

func GetMetadataEndpointRedirectHandler(ctx context.Context, authCtx interfaces.AuthenticationContext) http.HandlerFunc

This returns a handler that will redirect (303) to the well-known metadata endpoint for the OAuth2 authorization server See https://tools.ietf.org/html/rfc8414 for more information.

func GetOauth2Config

func GetOauth2Config(options config.OAuthOptions) (oauth2.Config, error)

This creates a oauth2 library config object, with values from the Flyte Admin config

func GetRefreshedToken

func GetRefreshedToken(ctx context.Context, oauth *oauth2.Config, accessToken, refreshToken string) (*oauth2.Token, error)

Refresh a JWT

func HashCsrfState

func HashCsrfState(csrf string) string

func NewCsrfCookie

func NewCsrfCookie() http.Cookie

func NewCsrfToken

func NewCsrfToken(seed int64) string

func NewRedirectCookie

func NewRedirectCookie(ctx context.Context, redirectURL string) *http.Cookie

This function takes in a string and returns a cookie that's used to keep track of where to send the user after the OAuth2 login flow is complete.

func NewSecureCookie

func NewSecureCookie(cookieName, value string, hashKey, blockKey []byte) (http.Cookie, error)

func ParseAndValidate

func ParseAndValidate(ctx context.Context, claims config.Claims, accessToken string,
	provider *oidc.Provider) (*oidc.IDToken, error)

func ReadSecureCookie

func ReadSecureCookie(ctx context.Context, cookie http.Cookie, hashKey, blockKey []byte) (string, error)

func RefreshTokensIfExists

func RefreshTokensIfExists(ctx context.Context, authContext interfaces.AuthenticationContext, handlerFunc http.HandlerFunc) http.HandlerFunc

Look for access token and refresh token, if both are present and the access token is expired, then attempt to refresh. Otherwise do nothing and proceed to the next handler. If successfully refreshed, proceed to the landing page.

func VerifyCsrfCookie

func VerifyCsrfCookie(ctx context.Context, request *http.Request) error

func WithUserEmail

func WithUserEmail(ctx context.Context, email string) context.Context

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

Please see the comment on the corresponding AuthenticationContext for more information.

func NewAuthenticationContext

func NewAuthenticationContext(ctx context.Context, options config.OAuthOptions) (Context, error)

func (Context) Claims

func (c Context) Claims() config.Claims

func (Context) CookieManager

func (c Context) CookieManager() interfaces.CookieHandler

func (Context) GetBaseURL

func (c Context) GetBaseURL() *url.URL

func (Context) GetHTTPClient

func (c Context) GetHTTPClient() *http.Client

func (Context) GetMetadataURL

func (c Context) GetMetadataURL() *url.URL

func (Context) GetUserInfoURL

func (c Context) GetUserInfoURL() *url.URL

func (Context) OAuth2Config

func (c Context) OAuth2Config() *oauth2.Config

func (Context) OidcProvider

func (c Context) OidcProvider() *oidc.Provider

func (Context) Options

func (c Context) Options() config.OAuthOptions

type CookieManager

type CookieManager struct {
	// contains filtered or unexported fields
}

func NewCookieManager

func NewCookieManager(ctx context.Context, hashKeyEncoded, blockKeyEncoded string) (CookieManager, error)

func (CookieManager) RetrieveTokenValues

func (c CookieManager) RetrieveTokenValues(ctx context.Context, request *http.Request) (accessToken string,
	refreshToken string, err error)

TODO: Separate refresh token from access token, remove named returns, and use stdlib errors.

func (CookieManager) SetTokenCookies

func (c CookieManager) SetTokenCookies(ctx context.Context, writer http.ResponseWriter, token *oauth2.Token) error

type CorsHandlerDecorator

type CorsHandlerDecorator func(http.Handler) http.Handler

These are here for CORS handling. Actual serving of the OPTIONS request will be done by the gorilla/handlers package

func GetCorsDecorator

func GetCorsDecorator(ctx context.Context, allowedOrigins, allowedHeaders []string) CorsHandlerDecorator

This produces a decorator that, when applied to an existing Handler, will first test if the request is an appropriate options request, and if so, serve it. If not, the underlying handler will be called.

type HTTPRequestToMetadataAnnotator

type HTTPRequestToMetadataAnnotator func(ctx context.Context, request *http.Request) metadata.MD

func GetHTTPRequestCookieToMetadataHandler

func GetHTTPRequestCookieToMetadataHandler(authContext interfaces.AuthenticationContext) HTTPRequestToMetadataAnnotator

This is effectively middleware for the grpc gateway, it allows us to modify the translation between HTTP request and gRPC request. There are two potential sources for bearer tokens, it can come from an authorization header (not yet implemented), or encrypted cookies. Note that when deploying behind Envoy, you have the option to look for a configurable, non-standard header name. The token is extracted and turned into a metadata object which is then attached to the request, from which the token is extracted later for verification.

type UserInfoResponse

type UserInfoResponse struct {
	Sub               string `json:"sub"`
	Name              string `json:"name"`
	PreferredUsername string `json:"preferred_username"`
	GivenName         string `json:"given_name"`
	FamilyName        string `json:"family_name"`
	Email             string `json:"email"`
	Picture           string `json:"picture"`
}

This struct represents what should be returned by an IDP according to the specification at

https://openid.net/specs/openid-connect-core-1_0.html#UserInfoResponse

Keep in mind that not all fields are necessarily populated, and additional fields may be present as well. This is a sample response object returned from Okta for instance

{
  "sub": "abc123",
  "name": "John Smith",
  "locale": "US",
  "preferred_username": "jsmith123@company.com",
  "given_name": "John",
  "family_name": "Smith",
  "zoneinfo": "America/Los_Angeles",
  "updated_at": 1568750854
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL