jwt

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: MIT Imports: 4 Imported by: 0

README

package auth/jwt

package auth/jwt provides a set of interfaces for service authorization through JSON Web Tokens without validating the signature. This needs to be used in cases where you know the signature is valid. This is go-kit/auth/jwt compatible package.

Usage

Use with HTTP Layer

import (
	jwt "github.com/dgrijalva/jwt-go"
	mojwt "github.com/mo-work/mo-kit/auth/jwt"

	kitjwt "github.com/go-kit/kit/auth/jwt"
	"github.com/go-kit/kit/endpoint"
)

type customClaims struct {
	Subject   string `json:"sub,omitempty"`
	Issuer    string `json:"iss,omitempty"`
	ExpiresAt int64  `json:"exp,omitempty"`
	IssuedAt  int64  `json:"iat,omitempty"`
	ID        string `json:"jti,omitempty"`
	TokenUse  string `json:"token_use,omitempty"`
	Scope     string `json:"scope,omitempty"`
	AuthTime  int64  `json:"auth_time,omitempty"`
	Version   int    `json:"version,omitempty"`
	ClientID  string `json:"client_id"`
}

func NewHTTPHandler(endpoints Endpoints, logger log.Logger, nr newrelic.Application) http.Handler {
	opts := []kithttp.ServerOption{
		kithttp.ServerErrorEncoder(encodeError),
		kithttp.ServerErrorLogger(logger),
		kithttp.ServerBefore(kitjwt.HTTPToContext()),
	}

	createHandler := kithttp.NewServer(
		mojwt.NewParserUnverified(jwt.SigningMethodRS256, func() jwt.Claims { return &customClaims{} })(endpoints.createEndpoint),
		decodeCreateRequest,
		encodeResponse,
		opts...,
	)
}

Retrieve the incoming information

import (
	"context"
	"fmt"

	"github.com/go-kit/kit/auth/jwt"
)

// Create creates the user object in the Database.
func (s *service) Create(ctx context.Context, user *User) (*User, error) {
	custCl, _ := ctx.Value(jwt.JWTClaimsContextKey).(*customClaims)
	if custCl == nil {
		return nil, jwt.ErrTokenInvalid
	}

	fmt.Println("CustomerID: ", custCl.ClientID)
	return user, nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewParserUnverified

func NewParserUnverified(method jwt.SigningMethod, newClaims kitjwt.ClaimsFactory) endpoint.Middleware

NewParserUnverified creates a new JWT token parsing middleware, the signing method and the claims type to be used. NewParserUnverified adds the resulting claims to endpoint context or returns error on invalid token. Particularly useful for servers.

func ParseUnverified

func ParseUnverified(tokenString string, claims jwt.Claims) (*jwt.Token, []string, error)

ParseUnverified parses the token but doesn't validate the signature. It's only ever useful in cases where you know the signature is valid (because it has been checked previously in the stack) and you want to extract values from it.

WARNING: Don't use this method unless you know what you're doing

Types

This section is empty.

Jump to

Keyboard shortcuts

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