Documentation
¶
Overview ¶
`grpcauth` is an authentication and authorization gRPC server side authentication wrappers.
Please see examples for simple examples of use.
Index ¶
- func DefaultAdminGroup() string
- func DefaultAdminGroups() []string
- func DefaultSuperAdminGroup() string
- func DefaultUserGroup() string
- func Header() string
- func Scheme() string
- type API
- func (api *API) AddAdminGroups(groups ...string)
- func (api *API) AddSuperAdminGroups(groups ...string)
- func (api *API) AdminGroups() []string
- func (api *API) Authenticator(ctx context.Context) (context.Context, error)
- func (api *API) AuthenticatorWithKey(ctx context.Context, signingKey []byte) (context.Context, error)
- func (api *API) AuthorizeGroups(ctx context.Context, groups ...string) (*Payload, error)
- func (api *API) AuthorizeIds(ctx context.Context, ids ...string) (*Payload, error)
- func (api *API) GenToken(ctx context.Context, payload *Payload, expirationTime time.Time) (string, error)
- func (api *API) GenTokenFromClaims(ctx context.Context, claims *Claims, expirationTime time.Time) (string, error)
- func (api *API) GenTokenUsingKey(ctx context.Context, claims *Claims, expirationTime time.Time, ...) (string, error)
- func (api *API) GetClaims(ctx context.Context) (*Claims, error)
- func (api *API) GetClaimsFromJwt(jwt string) (*Claims, error)
- func (api *API) GetMetadataFromCtx(ctx context.Context) metadata.MD
- func (api *API) GetMetadataFromJwt(jwt string) (metadata.MD, error)
- func (api *API) GetPayload(ctx context.Context) (*Payload, error)
- func (api *API) GetSigningKey() []byte
- func (api *API) IsAdmin(group string) bool
- func (api *API) IsGroupAllowed(group string, allowedGroups ...string) bool
- func (api *API) IsSuperAdmin(group string) bool
- func (api *API) SetPayloadProvider(provider PayloadProvider)
- type Claims
- type Payload
- type PayloadProvider
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultAdminGroup ¶
func DefaultAdminGroup() string
DefaultAdminGroup is the default admin group
func DefaultAdminGroups ¶
func DefaultAdminGroups() []string
DefaultAdminGroups returns the default administrators group
func DefaultSuperAdminGroup ¶
func DefaultSuperAdminGroup() string
DefaultSuperAdminGroup is the default super admin group
Types ¶
type API ¶
type API struct {
// contains filtered or unexported fields
}
func NewAPI ¶
NewAPI creates a JWT authentication and authorization helper using HS256.
Example ¶
api := NewAPI([]byte("secret"), "users", "users-api")
api.AddAdminGroups(DefaultAdminGroup())
api.AddSuperAdminGroups(DefaultSuperAdminGroup())
token, err := api.GenToken(context.Background(), &Payload{
ID: "user-1",
ProjectID: "project-1",
Group: DefaultUserGroup(),
}, time.Now().Add(time.Hour))
if err != nil {
panic(err)
}
md, err := api.GetMetadataFromJwt(token)
if err != nil {
panic(err)
}
_ = md
func (*API) AddAdminGroups ¶
AddAdminGroups appends groups treated as administrator groups.
func (*API) AddSuperAdminGroups ¶ added in v0.1.4
AddSuperAdminGroups appends groups treated as super-administrator groups.
func (*API) AdminGroups ¶
AdminGroups returns the configured admin and super-admin groups.
func (*API) Authenticator ¶ added in v0.0.3
Authenticator is the function that performs authentication
The passed in Context will contain the gRPC metadata.MD object (for header-based authentication) and the peer.Peer information that can contain transport-based credentials (e.g. credentials.AuthInfo).
The returned context will be propagated to handlers, allowing user changes to Context. However, please make sure that the Context returned is a child Context of the one passed in.
If error is returned, its grpc.Code() will be returned to the user as well as the verbatim message. Please make sure you use codes.Unauthenticated (lacking auth) and codes.PermissionDenied
func (*API) AuthenticatorWithKey ¶ added in v0.0.3
func (api *API) AuthenticatorWithKey(ctx context.Context, signingKey []byte) (context.Context, error)
AuthenticatorWithKey behaves like Authenticator but uses the supplied key.
func (*API) AuthorizeGroups ¶ added in v0.0.3
AuthorizeGroups checks whether the claims Group in the context metadata.MD Authorization JWT is a member the allowed groups set
If it's a member, Authorization will succeed, otherwise it will fail with codes.PermissionDenied.
The function will attempt to extract JWT token from gRPC metadata.MD Authorization key from the Context.
If getting metadata.MD object from Context fails i.e due to missing metadata.MD object OR missing Authorization key in the metadata.MD object, the function will fail with codes.Unauthenticated
It is expected that before calling this method, Authentication ought to have happened.
func (*API) AuthorizeIds ¶ added in v0.0.3
AuthorizeIds checks whether the claims Id in the context metadata.MD Authorization JWT is a member the allowed Ids set
If it's a member, Authorization will succeed, otherwise it will fail with codes.PermissionDenied.
The function will attempt to extract JWT token from gRPC metadata.MD Authorization key from the Context.
If getting metadata.MD object from Context fails i.e due to missing metadata.MD object OR missing Authorization key in the metadata.MD object, the function will fail with codes.Unauthenticated
It is expected that before calling this method, Authentication ought to have happened.
func (*API) GenToken ¶
func (api *API) GenToken(ctx context.Context, payload *Payload, expirationTime time.Time) (string, error)
GenToken generates JWT token with given payload that expire after expirationTime elapses.
It uses the receivers SigningMethod and SigningKey to sign the token.
func (*API) GenTokenFromClaims ¶
func (api *API) GenTokenFromClaims(ctx context.Context, claims *Claims, expirationTime time.Time) (string, error)
GenTokenFromClaims generates JWT token with given claims that expire after expirationTime elapses.
It uses the receivers SigningMethod and default secret to sign the token.
func (*API) GenTokenUsingKey ¶
func (api *API) GenTokenUsingKey(ctx context.Context, claims *Claims, expirationTime time.Time, signingKey []byte) (string, error)
GenTokenUsingKey generates JWT token with given payload that expire after expirationTime elapses.
It uses the provided signingKey and the receiver SigningMethod to sign the token.
func (*API) GetClaimsFromJwt ¶
GetClaimsFromJwt parses a token string using the API signing key.
func (*API) GetMetadataFromCtx ¶
GetMetadataFromCtx returns incoming gRPC metadata from ctx, or an empty map if none exists.
func (*API) GetMetadataFromJwt ¶
GetMetadataFromJwt constructs incoming gRPC metadata for a bearer token.
func (*API) GetPayload ¶ added in v0.0.3
GetPayload returns the authenticated payload stored in ctx.
func (*API) GetSigningKey ¶ added in v0.0.3
GetSigningKey returns the signing key configured for the API.
func (*API) IsGroupAllowed ¶ added in v0.1.4
IsGroupAllowed reports whether group is in allowedGroups.
func (*API) IsSuperAdmin ¶ added in v0.1.4
IsSuperAdmin reports whether group is in any configured super-admin group.
func (*API) SetPayloadProvider ¶ added in v1.0.1
func (api *API) SetPayloadProvider(provider PayloadProvider)
SetPayloadProvider sets the provider used to fetch full payload data (e.g. from MySQL/Redis) for JWTs that only contain the user ID.
type Claims ¶
type Claims struct {
*Payload
jwt.StandardClaims
}
Claims contains JWT claims information