gotoken

package module
v0.0.0-...-a34ed3a Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-token

go-token is a package (and sub-packages) providing functions for getting up and running with long-term access tokens.

try it out with the examples folder, e.g.: go run examples/simple_no_db/simple-no-db-main.go

For root tokens (one "master" token)

  • A Small program to generate a base64-encoded root token and the base64 encoded hash for it
  • It provides a middleware for authorizing root tokens

For auth tokens (many tokens)

  • Web service to create auth tokens
  • Middleware for authorizing token
  • Helper functions for retrieving token ID and account ID from the request context

Documentation

Index

Constants

View Source
const (
	JwtIDKey        = "id"
	JwtAccountIDKey = "account"
	JwtRoleIDsKey   = "roles"
)

Variables

View Source
var (
	TokenIDCtxKey        = ctxKey{Name: "TokenID"}
	TokenAccountIDCtxKey = ctxKey{Name: "AccountID"}
	TokenRoleIDsCtxKey   = ctxKey{Name: "RoleIDs"}
)

Functions

func CheckHasRole

func CheckHasRole(ctx context.Context, logger *logpkg.Logger, w http.ResponseWriter, r *http.Request, roleID int64) bool

CheckHasRole checks if a user has a given role. It returns a boolean: if the execution should continue or not. If the user doesn't have the role it, logs to the logger and returns 403. If there is any other error, it returns 500

func GetAccountIDFromCtx

func GetAccountIDFromCtx(ctx context.Context) (int64, errorsx.Error)

func GetIDFromCtx

func GetIDFromCtx(ctx context.Context) (int64, errorsx.Error)

func GetRoleIDsFromCtx

func GetRoleIDsFromCtx(ctx context.Context) ([]int64, errorsx.Error)

func HasRoleID

func HasRoleID(ctx context.Context, roleID int64) (bool, errorsx.Error)

Types

type Token

type Token struct {
	// ID of the token (if required)
	ID int64
	// ID of the "account" the token belongs to (probably creaated by)
	// This could be e.g. a user ID, or an organisation ID
	AccountID int64
	Name      string
	// RoleIDs the token has. This can be used the user has access to the endpoint they are trying to access
	RoleIDs   []int64
	CreatedAt time.Time
}

func NewToken

func NewToken(id, accountID int64, name string, roleIDs []int64, createdAt time.Time) *Token

func (*Token) ToJWTToken

func (token *Token) ToJWTToken(hmacSecret []byte) (string, errorsx.Error)

Directories

Path Synopsis
scripts

Jump to

Keyboard shortcuts

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