authn

package
v3.35.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: Apache-2.0, Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package authn handles authentication

Copyright 2022 Molecula Corp (DBA FeatureBase). All rights reserved.

Index

Constants

View Source
const (
	// AccessCookieName is the name of the cookie that holds the access token.
	AccessCookieName = "molecula-chip"

	// RefreshCookieName is the name of the cookie that holds the refresh token.
	RefreshCookieName = "refresh-molecula-chip"

	// RefreshHeaderName is the name of the header that holds the refresh token.
	RefreshHeaderName = "X-Molecula-Refresh-Token"

	// ContextValueAccessToken is the key used to set AccessTokens in a ctx.
	ContextValueAccessToken = AuthContextKey("Access")

	// ContextValueRefreshToken is the key used to set RefreshTokens in a ctx.
	ContextValueRefreshToken = AuthContextKey("Refresh")
)

Variables

This section is empty.

Functions

func GetAccessToken

func GetAccessToken(ctx context.Context) (token string, ok bool)

GetAccessToken gets the access token from a context.

func GetIndexes

func GetIndexes(ctx context.Context) (indexes []string, ok bool)

GetIndexes get the indices from a context.

func GetRefreshToken

func GetRefreshToken(ctx context.Context) (token string, ok bool)

GetRefreshToken gets the refresh token from a context.

func WithAccessToken

func WithAccessToken(ctx context.Context, token string) context.Context

WithAccessToken makes a new Context with an access token.

func WithIndexes

func WithIndexes(ctx context.Context, indexes []string) context.Context

WithIndexes makes a new Context with a []string containing the indicies.

func WithRefreshToken

func WithRefreshToken(ctx context.Context, token string) context.Context

WithRefreshToken makes a new Context with a refresh token.

func WithUserInfo

func WithUserInfo(ctx context.Context, userInfo *UserInfo) context.Context

WithUserInfo makes a new Context with UserInfo.

Types

type Auth

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

Auth holds state, configuration, and utilities needed for authentication.

func NewAuth

func NewAuth(logger logger.Logger, url string, scopes []string, authURL, tokenURL, groupEndpoint, logout, clientID, clientSecret, secretKey string, configuredIPs []string) (auth *Auth, err error)

NewAuth instantiates and returns a new Auth struct

func (*Auth) Authenticate

func (a *Auth) Authenticate(access, refresh string) (*UserInfo, error)

Authenticate takes in a auth token `access` and returns UserInfo from that token it is caller's responsibility to inform the user that the access token has been refreshed

func (*Auth) CheckAllowedNetworks

func (a *Auth) CheckAllowedNetworks(clientIP string) bool

if IP is in allowed networks, then return true to grant admin permissions

func (Auth) CleanOAuthConfig

func (a Auth) CleanOAuthConfig() oauth2.Config

CleanOAuthConfig returns a's oauthConfig without the client secret

func (*Auth) Login

func (a *Auth) Login(w http.ResponseWriter, r *http.Request)

Login redirects a user to login to their configured oAuth authorize endpoint

func (*Auth) Logout

func (a *Auth) Logout(w http.ResponseWriter, r *http.Request)

Logout clears out the user's cookie, removes the token from our cache, and redirects user to IdP's logout endpoint

func (*Auth) Redirect

func (a *Auth) Redirect(w http.ResponseWriter, r *http.Request)

Redirect handles the oAuth /redirect endpoint. It gets an access token and returns it to the user in the form of a cookie

func (Auth) SecretKey

func (a Auth) SecretKey() []byte

SecretKey is a convenient function to get the SecretKey from an Auth struct

func (*Auth) SetCookie

func (a *Auth) SetCookie(w http.ResponseWriter, access, refresh string, expiry time.Time) error

func (*Auth) SetGRPCMetadata

func (a *Auth) SetGRPCMetadata(ctx context.Context, md metadata.MD, access, refresh string) (context.Context, error)

type AuthContextKey

type AuthContextKey string

AuthContextKey is a unique type to prevent collisions when using context.WithValue()

type Group

type Group struct {
	GroupID   string `json:"id"`
	GroupName string `json:"displayName"`
}

Group holds group information for an authenticated user

type Groups

type Groups struct {
	NextLink string  `json:"@odata.nextLink"`
	Groups   []Group `json:"value"`
}

Groups holds a slice of Group for marshalling from JSON

type UserInfo

type UserInfo struct {
	UserID       string    `json:"userid"`
	UserName     string    `json:"username"`
	Groups       []Group   `json:"groups"`
	Expiry       time.Time `json:"expiry"`
	Token        string    `json:"token"`
	RefreshToken string    `json:"refreshtoken"`
}

UserInfo holds the information about the user from the token

func GetUserInfo

func GetUserInfo(ctx context.Context) (userInfo *UserInfo, ok bool)

GetUserInfo gets the UserInfo from a context.

Jump to

Keyboard shortcuts

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