oauth

package module
v0.0.0-...-49ab10e Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2021 License: MIT Imports: 11 Imported by: 0

README

oauth middleware

OAuth 2.0 Authorization Server & Authorization Middleware for Gin-Gonic

This library offers an OAuth 2.0 Authorization Server based on Gin-Gonic and an Authorization Middleware usable in Resource Servers developed with Gin-Gonic.

Build status

Build Status

Authorization Server

The Authorization Server is implemented by the struct OAuthBearerServer that manages two grant types of authorizations (password and client_credentials). This Authorization Server is made to provide an authorization token usable for consuming resources API.

Password grant type

OAuthBearerServer supports the password grant type, allowing the token generation for username / password credentials.

Client Credentials grant type

OAuthBearerServer supports the client_credentials grant type, allowing the token generation for appid / secret credentials.

Authorization Code and Implicit grant type

These grant types are currently partially supported implementing AuthorizationCodeVerifier interface. The method ValidateCode is called during the phase two of the authorization_code grant type evalutations.

Refresh token grant type

If authorization token will expire, the client can regenerate the token calling the authorization server and using the refresh_token grant type.

Authorization Middleware

The Gin-Gonic middleware BearerAuthentication intercepts the resource server calls and authorizes only resource requests containing a valid bearer token.

Token Formatter

Authorization Server crypts the token using the Token Formatter and Authorization Middleware decrypts the token using the same Token Formatter. This library contains a default implementation of the formatter interface called _ SHA256RC4TokenSecureFormatter_ based on the algorithms SHA256 and RC4. Programmers can develop their Token Formatter implementing the interface TokenSecureFormatter and this is really recommended before publishing the API in a production environment.

Credentials Verifier

The interface CredentialsVerifier defines the hooks called during the token generation process. The methods are called in this order:

  • ValidateUser() or ValidateClient() called first for credentials verification

  • AddClaims() used for add information to the token that will be encrypted

  • StoreTokenId() called after the token generation but before the response, programmers can use this method for storing the generated Ids There is another method in the CredentialsVerifier interface that is involved during the refresh token process. In this case the methods are called in this order:

  • ValidateTokenId() called first for TokenId verification, the method receives the TokenId related to the token associated to the refresh token

  • AddClaims() used for add information to the token that will be encrypted

  • StoreTokenId() called after the token regeneration but before the response, programmers can use this method for storing the generated Ids

Authorization Server usage example

This snippet shows how to create an authorization server

func main() {
router := gin.New()
router.Use(gin.Recovery())
router.Use(gin.Logger())

s := oauth.NewOAuthBearerServer(
"mySecretKey-10101",
time.Second*120,
&TestUserVerifier{},
nil)
router.POST("/token", s.UserCredentials)
router.POST("/auth", s.ClientCredentials)

router.Run(":9090")
}

See test.txt for the full example.

Authorization Middleware usage example

This snippet shows how to use the middleware

    authorized := router.Group("/")
// use the Bearer Athentication middleware
authorized.Use(oauth.Authorize("mySecretKey-10101", nil))

authorized.GET("/customers", GetCustomers)
authorized.GET("/customers/:id/orders", GetOrders)

See test.txt for the full example.

Note that the authorization server and the authorization middleware are both using the same token formatter and the same secret key for encryption/decryption.

Note

This master branch introduces breaking changes in the interface CredentialsVerifier methods ValidateUser, _ ValidateClient_ and AddClaims. Refer to v1 branch for the previous implementation. Updated server implementation in v3 due to go.uuid library change.

Reference

License

MIT

Documentation

Index

Constants

View Source
const (
	TOKEN_TYPE = "Bearer"
)

Variables

This section is empty.

Functions

func Authorize

func Authorize(secretKey string, formatter TokenSecureFormatter) gin.HandlerFunc

Authorize is the OAuth 2.0 middleware for Gin-Gonic resource server. Authorize creates a BearerAuthentication middlever and return the Authorize method.

func CheckBasicAuthentication

func CheckBasicAuthentication(username, password string, ctx *gin.Context) error

Check Basic Autrhorization header credentials

func Fail

func Fail(c *gin.Context, code, message interface{})

func GetBasicAuthentication

func GetBasicAuthentication(ctx *gin.Context) (username, password string, err error)

GetBasicAuthentication get username and password from Authorization header

func Successful

func Successful(c *gin.Context, data interface{})

Types

type Any

type Any interface{}

type AuthorizationCodeVerifier

type AuthorizationCodeVerifier interface {
	// ValidateCode checks the authorization code and returns the user credential
	ValidateCode(appid, secret, code, redirectURI string, req *http.Request) (string, error)
}

AuthorizationCodeVerifier defines the interface of the Authorization Code verifier

type BearerAuthentication

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

BearerAuthentication middleware for Gin-Gonic

func NewBearerAuthentication

func NewBearerAuthentication(secretKey string, formatter TokenSecureFormatter) *BearerAuthentication

NewBearerAuthentication create a BearerAuthentication middleware

func (*BearerAuthentication) Authorize

func (ba *BearerAuthentication) Authorize(ctx *gin.Context)

Authorize verifies the bearer token authorizing or not the request. Token is retreived from the Authorization HTTP header that respects the format Authorization: Bearer {access_token}

type CredentialsVerifier

type CredentialsVerifier interface {
	// Validate username and password returning an error if the user credentials are wrong
	ValidateUser(username, password, scope string, req *http.Request) error
	// Validate appid and secret returning an error if the client credentials are wrong
	ValidateClient(appid, secret, scope string, req *http.Request) error
	// Provide additional claims to the token
	AddClaims(credential, tokenID, tokenType, scope string) (map[string]string, error)
	// Optionally store the tokenID generated for the user
	StoreTokenId(credential, tokenID, refreshTokenID, tokenType string) error
	// Optionally validate previously stored tokenID during refresh request
	ValidateTokenId(credential, tokenID, refreshTokenID, tokenType string) error
	// Response_type=code Verify whether the code obtained is legal
	ValidateResponseCode(appid, redirectURI, state, scope string) (int, interface{})
}

CredentialsVerifier defines the interface of the user and client credentials verifier.

type OAuthBearerServer

type OAuthBearerServer struct {
	TokenTTL time.Duration
	// contains filtered or unexported fields
}

OAuthBearerServer is the OAuth 2 Bearer Server implementation.

func NewOAuthBearerServer

func NewOAuthBearerServer(secretKey string,
	ttl time.Duration,
	verifier CredentialsVerifier,
	formatter TokenSecureFormatter) *OAuthBearerServer

NewOAuthBearerServer creates new OAuth 2 Bearer Server

func (*OAuthBearerServer) AuthAccessToken

func (s *OAuthBearerServer) AuthAccessToken(ctx *gin.Context)

AuthAccessToken get token

func (*OAuthBearerServer) AuthRefreshToken

func (s *OAuthBearerServer) AuthRefreshToken(ctx *gin.Context)

AuthRefreshToken refresh token

func (*OAuthBearerServer) AuthorizationCode

func (s *OAuthBearerServer) AuthorizationCode(ctx *gin.Context)

AuthorizationCode manages authorization code grant type requests for the phase two of the authorization process

func (*OAuthBearerServer) Authorize

func (s *OAuthBearerServer) Authorize(ctx *gin.Context)

AuthAccessToken get token

func (*OAuthBearerServer) ClientCredentials

func (s *OAuthBearerServer) ClientCredentials(ctx *gin.Context)

ClientCredentials manages client credentials grant type requests

func (*OAuthBearerServer) UserCredentials

func (s *OAuthBearerServer) UserCredentials(ctx *gin.Context)

UserCredentials manages password grant type requests

type RC4TokenSecureFormatter

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

func NewRC4TokenSecurityProvider

func NewRC4TokenSecurityProvider(key []byte) *RC4TokenSecureFormatter

func (*RC4TokenSecureFormatter) CryptToken

func (sc *RC4TokenSecureFormatter) CryptToken(source []byte) ([]byte, error)

func (*RC4TokenSecureFormatter) DecryptToken

func (sc *RC4TokenSecureFormatter) DecryptToken(source []byte) ([]byte, error)

type RefreshToken

type RefreshToken struct {
	CreationDate   time.Time `json:"date"`
	TokenId        string    `json:"id_token"`
	RefreshTokenId string    `json:"id_refresh_token"`
	Credential     string    `json:"credential"`
	TokenType      string    `json:"type"` // "U" for user, "C" for client
	Scope          string    `json:"scope"`
}

RefreshToken structure included in the authorization server response

type SHA256RC4TokenSecureFormatter

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

func NewSHA256RC4TokenSecurityProvider

func NewSHA256RC4TokenSecurityProvider(key []byte) *SHA256RC4TokenSecureFormatter

func (*SHA256RC4TokenSecureFormatter) CryptToken

func (sc *SHA256RC4TokenSecureFormatter) CryptToken(source []byte) ([]byte, error)

func (*SHA256RC4TokenSecureFormatter) DecryptToken

func (sc *SHA256RC4TokenSecureFormatter) DecryptToken(source []byte) ([]byte, error)

type Token

type Token struct {
	Id           string            `json:"id_token"`
	CreationDate time.Time         `json:"date"`
	ExperesIn    time.Duration     `json:"expires_in"` // secs
	Credential   string            `json:"credential"`
	Scope        string            `json:"scope"`
	Claims       map[string]string `json:"claims"`
	TokenType    string            `json:"type"` // "U" for user, "C" for client
}

Token structure generated by the authorization server

type TokenProvider

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

func NewTokenProvider

func NewTokenProvider(formatter TokenSecureFormatter) *TokenProvider

func (*TokenProvider) CryptRefreshToken

func (tp *TokenProvider) CryptRefreshToken(t *RefreshToken) (token string, err error)

func (*TokenProvider) CryptToken

func (tp *TokenProvider) CryptToken(t *Token) (token string, err error)

func (*TokenProvider) DecryptRefreshTokens

func (tp *TokenProvider) DecryptRefreshTokens(refreshToken string) (refresh *RefreshToken, err error)

func (*TokenProvider) DecryptToken

func (tp *TokenProvider) DecryptToken(token string) (t *Token, err error)

type TokenResponse

type TokenResponse struct {
	Token        string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	TokenType    string `json:"token_type"` // bearer
	ExperesIn    int64  `json:"expires_in"` // secs
}

TokenResponse is the authorization server response

type TokenSecureFormatter

type TokenSecureFormatter interface {
	CryptToken(source []byte) ([]byte, error)
	DecryptToken(source []byte) ([]byte, error)
}

Jump to

Keyboard shortcuts

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