authentication

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAuthenticationNotFound = errors.New("authentication service not found in context")
	ErrInvalidToken           = errors.New("invalid token")
	ErrInvalidSource          = errors.New("invalid source")
	ErrInvalidSignature       = errors.New("invalid signature")
	ErrInvalidPermissions     = errors.New("invalid permissions")
	ErrTokenExpired           = errors.New("token expired")
)
View Source
var (
	ErrMalformedToken = errors.New("malformed token")
)

Functions

func ContextWithService

func ContextWithService(ctx context.Context) (context.Context, error)

ContextWithService returns a context with the authentication service.

Types

type AuthenticationResult

type AuthenticationResult struct {
	// Authenticated is true if the token was authenticated.
	Authenticated bool
	// Token is the token that was authenticated.
	Token *Token
}

AuthenticationResult is the result of authenticating a token.

type Authorization

type Authorization struct {
	// Type is the type of authorization, either action or receiver.
	Type AuthorizationType
	// Operations is the set of authorized operations.
	Operations []AuthorizedOperation
	// Name is the name of the action or receiver.
	Name string
}

Authorization is a set of authorized operations.

type AuthorizationRequest

type AuthorizationRequest struct {
	// Type is the type of authorization.
	Type AuthorizationType
	// Operation is the operation to check.
	Operation AuthorizedOperation
	// Name is the name of the action or receiver.
	Name string
}

AuthorizationRequest is a request to check authorization.

type AuthorizationType

type AuthorizationType int

AuthorizationType is the type of authorization.

const (
	Action AuthorizationType = iota
	Receiver
)

type AuthorizedOperation

type AuthorizedOperation int

AuthorizedOperation is an operation that is authorized.

const (
	Run AuthorizedOperation = iota
	Publish
	Subscribe
	Notify
)

type Claims

type Claims struct {
	AuthorizedNetwork net.IPNet
	Permitted         int32
	Authorizations    []Authorization
	Expiry            time.Time
}

Claims is a set of claims that can be used to issue a token.

func (Claims) IsAuthorized

func (c Claims) IsAuthorized(req AuthorizationRequest) bool

IsAuthorized checks if the claims are authorized for the provided request.

func (Claims) Permissions

func (c Claims) Permissions() permission.Permissions

Permissions returns the permissions of the claims.

type Service

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

func GetService

func GetService(ctx context.Context) (*Service, error)

GetService returns the authentication service from the context.

func (*Service) Authenticate

func (s *Service) Authenticate(ctx context.Context, tokenStr string, source net.IP) (*AuthenticationResult, error)

Authenticate authenticates a token.

func (*Service) Issue

func (s *Service) Issue(ctx context.Context, claims Claims) (*Token, error)

Issue issues a token with the provided claims.

type Token

type Token struct {
	// Claims is the claims of the token.
	Claims
	// Signature is the signature of the token.
	Signature []byte
}

Token is a token that can be used to authenticate a peer.

func CreateToken added in v0.0.3

func CreateToken(ctx context.Context, key []byte, claims Claims) (*Token, error)

CreateToken creates a token with the provided claims and key.

func NewToken

func NewToken(data Claims, key []byte) (*Token, error)

NewToken creates a new token with the provided claims and key.

func NewTokenFromString

func NewTokenFromString(data string) (*Token, error)

NewTokenFromString creates a new token from the provided string.

func (*Token) MarshalString

func (s *Token) MarshalString() (string, error)

MarshalString marshals the token into a string.

func (*Token) String

func (t *Token) String() string

func (*Token) VerifySignature

func (s *Token) VerifySignature(key []byte) error

VerifySignature verifies the signature of the token.

Jump to

Keyboard shortcuts

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