lcom

package
v1.3.7 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package lcom contains common vars, consts, and types that are used throughout the program. Additionally, users who download and install this library should investigate the contents of this package to get the most out of the library and its functionality.

Index

Constants

View Source
const CORSHeadersEnvKey = "LAMBDA_JWT_ROUTER_CORS_HEADERS"
View Source
const CORSHeadersHeaderKey = "Access-Control-Allow-Headers"
View Source
const CORSMethodsEnvKey = "LAMBDA_JWT_ROUTER_CORS_METHODS"
View Source
const CORSMethodsHeaderKey = "Access-Control-Allow-Methods"
View Source
const CORSOriginEnvKey = "LAMBDA_JWT_ROUTER_CORS_ORIGIN"
View Source
const CORSOriginHeaderKey = "Access-Control-Allow-Origin"
View Source
const ContentTypeKey = "Content-Type"

ContentTypeKey exists because "Content-Type" is not in the http std lib for some reason...

View Source
const HMACSecretEnvKey = "LAMBDA_JWT_ROUTER_HMAC_SECRET"
View Source
const JWTClaimAudienceKey = "aud"
View Source
const JWTClaimEmailKey = "email"
View Source
const JWTClaimExpiresAtKey = "exp"
View Source
const JWTClaimFirstNameKey = "firstName"
View Source
const JWTClaimFullNameKey = "fullName"
View Source
const JWTClaimIDKey = "jti"
View Source
const JWTClaimIssuedAtKey = "iat"
View Source
const JWTClaimIssuerKey = "iss"
View Source
const JWTClaimLevelKey = "level"
View Source
const JWTClaimNotBeforeKey = "nbf"
View Source
const JWTClaimSubjectKey = "sub"
View Source
const JWTClaimUserTypeKey = "userType"
View Source
const LambdaContextIDKey = "id"
View Source
const LambdaContextMethodKey = "method"
View Source
const LambdaContextMultiParamsKey = "multiParams"
View Source
const LambdaContextPathKey = "path"
View Source
const LambdaContextPathParamsKey = "pathParams"
View Source
const LambdaContextQueryParamsKey = "queryParams"
View Source
const LambdaContextRequestIDKey = "requestId"
View Source
const LambdaContextUserIDKey = "userId"
View Source
const LambdaContextUserTypeKey = "userType"
View Source
const NoCORS = "LAMBDA_JWT_ROUTER_NO_CORS"

Variables

View Source
var ErrBadClaimsObject = errors.New("lambda_jwt_router: the provided object to extract claims into is not compatible with the default claim set and its types: %w")
View Source
var ErrInvalidJWT = errors.New("lambda_jwt_router: the provided JWT is invalid: %w")
View Source
var ErrInvalidToken = errors.New("lambda_jwt_router: the provided jwt was unable to be parsed into a token: %w")
View Source
var ErrInvalidTokenClaims = errors.New("lambda_jwt_router: the provided jwt was unable to be parsed for map claims: %w")
View Source
var ErrMarshalMapClaims = errors.New("unable to Marshal map claims: %w")
View Source
var ErrNoAuthorizationHeader = errors.New("no Authorization header value set: %w")
View Source
var ErrNoBearerPrefix = errors.New("missing 'Bearer ' prefix for Authorization header value: %w")
View Source
var ErrUnableToSignToken = errors.New("lambda_jwt_router: the provided claims were unable to be signed: %w")
View Source
var ErrUnsupportedSigningMethod = errors.New("lambda_jwt_router:the provided signing method is unsupported. HMAC only allowed: %w")
View Source
var ErrVerifyJWT = errors.New("unable to verify JWT to retrieve claims. try logging in again to ensure it is not expired: %w")

Functions

This section is empty.

Types

type Handler

Handler is a lambda request handler function. It takes in the context value created by API Gateway when proxying to AWS Lambda in addition to the events.APIGatewayProxyRequest event itself. This request object is created by API Gateway and has all the requisite fields set automagically. When running this library locally, it will automagically fill in the lambda request object manually so that your local code will mirror exactly what locally when it's run remotely in the AWS Lambda environment. An error value should be returned as well. Note that your application's errors should never set the error value here but instead return an error message or type embedded in the body of the response. This error is for critical stack crashes / go crashes. Example:

 func listSomethings(ctx context.Context, req events.APIGatewayProxyRequest) (res events.APIGatewayProxyResponse err error) {
     // parse input
     var input listSomethingsInput
     err = UnmarshalReq(req, false, &input)
     if err != nil {
         return ErrorRes(err)
     }

     // call some business logic that generates an output struct
     // ...

     return SuccessRes(result)
}

type LambdaParams added in v1.3.4

type LambdaParams struct {
	IDPath        string `lambda:"path.id"`
	IDQuery       string `lambda:"query.id"`
	UserIDBody    string `json:"userId"`
	UserIDPath    string `lambda:"path.userId"`
	UserIDQuery   string `lambda:"query.userId"`
	UserTypeBody  string `json:"userType"`
	UserTypePath  string `lambda:"path.userType"`
	UserTypeQuery string `lambda:"query.userType"`
}

func (*LambdaParams) GetID added in v1.3.4

func (lp *LambdaParams) GetID() string

func (*LambdaParams) GetOwnerID added in v1.3.4

func (lp *LambdaParams) GetOwnerID() string

func (*LambdaParams) GetUserID added in v1.3.4

func (lp *LambdaParams) GetUserID() string

func (*LambdaParams) GetUserType added in v1.3.4

func (lp *LambdaParams) GetUserType() string

type Middleware

type Middleware func(Handler) Handler

Middleware is a function that receives a handler function (the next function in the chain, possibly another middleware or the actual handler matched for a req), and returns a handler function. These functions are quite similar to HTTP middlewares in other libraries.

Example middleware that logs all reqs:

func loggerMiddleware(next lmdrouter.Handler) lmdrouter.Handler {
    return func(ctx context.Context, req events.APIGatewayProxyRequest) (
        res events.APIGatewayProxyResponse,
        err error,
    ) {
        format := "[%s] [%s %s] [%d]%s"
        level := "INF"
        var code int
        var extra string

        res, err = next(ctx, req)
        if err != nil {
            level = "ERR"
            code = http.StatusInternalServerError
            extra = " " + err.ErrorRes()
        } else {
            code = res.StatusCode
            if code >= 400 {
                level = "ERR"
            }
        }

        log.Printf(format, level, req.HTTPMethod, req.Path, code, extra)

        return res, err
    }
}

Jump to

Keyboard shortcuts

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