jwt

package module
Version: v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2021 License: MIT Imports: 7 Imported by: 0

README

JWT

Go GoDoc

A simple JWT decoder.

Usage

Parsing URLs
func ExampleParseURL() {
	u := "https://jwt.ms/?state=d61b0af6-4704-4070-b854-ee5feab01bf2&access_token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTQ1ODY5ODIsIm5iZiI6MTYxNDU4MzM4MiwidmVyIjoiMS4wIiwiaXNzIjoiaHR0cHM6Ly9nb2xsYWhhbGxpYXV0aC5iMmNsb2dpbi5jb20vNDkyMDY1NmQtMzZlMC00YjM1LTg5NGQtMGEwYTE0YmVjOGIzL3YyLjAvIiwic3ViIjoiNzYyNWY0MDktMWZhNy00ZDc4LTkzYjAtNWMzNDAyNTVjZGYyIiwiYXVkIjoiMTIzNDU2Nzg5Iiwibm9uY2UiOiJkZWZhdWx0Tm9uY2UiLCJpYXQiOjE2MTQ1ODMzODIsImF1dGhfdGltZSI6MTYxNDU4MzM4Miwib2lkIjoic29tZS1vaWQiLCJnaXZlbl9uYW1lIjoiQWtzaGF5IiwiZmFtaWx5X25hbWUiOiJHb2xsYWhhbGxpIiwidGZwIjoiQjJDXzFfU2lnblVwU2lnbkluRmxvdyJ9.CNbe3qUhpUR7U51e9k1miAdQP3ZEbCDip67MlvgTpFsFmB3nbjp7wi8e-66cPoS9z_hmQP3wLc5I8KE-b4_cFCzHZkhuQPA0Mhi9mBAZ_tAPSXNaDeiX1FEalsiH_sWuHWnojxSwlSxeKL9Tlh_0u5vXaABILdDeRWOTBJDHZ5I2BgIk8J_hI-fXDvBb0wfjI4mQe7lQqDZLVos4mlA1Uhpz2wN7Lorc31PZo02STbk5S1j1BMk4eQUS_OHTKZfAlUmfV9giWEMr7qk21eSy_HlybVKQgCB9Cde_rmNWJETyw6X712QGYWaDkDrwocQ99wR6rALWVkkTOhmL6CRT5Q&token_type=Bearer&expires_in=3600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tMGEwYTE0YmVjOGIzL3YyLjAvIiwic3ViIjoiNzYyNWY0MDktMWZhNy00ZDc4LTkzYjAtNWMzNDAyNTVjZGYyIiwiYXVkIjoiMTIzNDU2Nzg5Iiwibm9uY2UiOiJkZWZhdWx0Tm9uY2UiLCJpYXQiOjE2MTQ1ODMzODIsImF1dGhfdGltZSI6MTYxNDU4MzM4Miwib2lkIjoic29tZS1vaWQiLCJnaXZlbl9uYW1lIjoiQWtzaGF5IiwiZmFtaWx5X25hbWUiOiJHb2xsYWhhbGxpIiwidGZwIjoiQjJDXzFfU2lnblVwU2lnbkluRmxvdyJ9.CNbe3qUhpUR7U51e9k1miAdQP3ZEbCDip67MlvgTpFsFmB3nbjp7wi8e-66cPoS9z_hmQP3wLc5I8KE-b4_cFCzHZkhuQPA0Mhi9mBAZ_tAPSXNaDeiX1FEalsiH_sWuHWnojxSwlSxeKL9Tlh_0u5vXaABILdDeRWOTBJDHZ5I2BgIk8J_hI-fXDvBb0wfjI4mQe7lQqDZLVos4mlA1Uhpz2wN7Lorc31PZo02STbk5S1j1BMk4eQUS_OHTKZfAlUmfV9giWEMr7qk21eSy_HlybVKQgCB9Cde_rmNWJETyw6X712QGYWaDkDrwocQ99wR6rALWVkkTOhmL6CRT5Q"
	c := "123456789"
	var data providers.AzureB2C
	token, err := ParseURL(u, c)
	if err != nil {
		panic(err)
	}
	err = token.UnmarshalAccessToken(&data)
	if err != nil {
		panic(err)
	}
	fmt.Println(data)
	// Output: {1614586982 1614583382 1.0 https://gollahalliauth.b2clogin.com/4920656d-36e0-4b35-894d-0a0a14bec8b3/v2.0/ 7625f409-1fa7-4d78-93b0-5c340255cdf2 123456789 defaultNonce 1614583382 1614583382 some-oid Akshay Gollahalli B2C_1_SignUpSignInFlow    }
}
Parsing Tokens
func ExampleParseAccessToken() {
	u := "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTQ1ODY5ODIsIm5iZiI6MTYxNDU4MzM4MiwidmVyIjoiMS4wIiwiaXNzIjoiaHR0cHM6Ly9nb2xsYWhhbGxpYXV0aC5iMmNsb2dpbi5jb20vNDkyMDY1NmQtMzZlMC00YjM1LTg5NGQtMGEwYTE0YmVjOGIzL3YyLjAvIiwic3ViIjoiNzYyNWY0MDktMWZhNy00ZDc4LTkzYjAtNWMzNDAyNTVjZGYyIiwiYXVkIjoiMTIzNDU2Nzg5Iiwibm9uY2UiOiJkZWZhdWx0Tm9uY2UiLCJpYXQiOjE2MTQ1ODMzODIsImF1dGhfdGltZSI6MTYxNDU4MzM4Miwib2lkIjoic29tZS1vaWQiLCJnaXZlbl9uYW1lIjoiQWtzaGF5IiwiZmFtaWx5X25hbWUiOiJHb2xsYWhhbGxpIiwidGZwIjoiQjJDXzFfU2lnblVwU2lnbkluRmxvdyJ9.CNbe3qUhpUR7U51e9k1miAdQP3ZEbCDip67MlvgTpFsFmB3nbjp7wi8e-66cPoS9z_hmQP3wLc5I8KE-b4_cFCzHZkhuQPA0Mhi9mBAZ_tAPSXNaDeiX1FEalsiH_sWuHWnojxSwlSxeKL9Tlh_0u5vXaABILdDeRWOTBJDHZ5I2BgIk8J_hI-fXDvBb0wfjI4mQe7lQqDZLVos4mlA1Uhpz2wN7Lorc31PZo02STbk5S1j1BMk4eQUS_OHTKZfAlUmfV9giWEMr7qk21eSy_HlybVKQgCB9Cde_rmNWJETyw6X712QGYWaDkDrwocQ99wR6rALWVkkTOhmL6CRT5Q"
	c := "123456789"
	var data providers.AzureB2C
	token := ParseAccessToken(u, c)
	err := token.UnmarshalAccessToken(&data)
	if err != nil {
		panic(err)
	}
	fmt.Println(data)
	// Output: {1614586982 1614583382 1.0 https://gollahalliauth.b2clogin.com/4920656d-36e0-4b35-894d-0a0a14bec8b3/v2.0/ 7625f409-1fa7-4d78-93b0-5c340255cdf2 123456789 defaultNonce 1614583382 1614583382 some-oid Akshay Gollahalli B2C_1_SignUpSignInFlow    }
}

Security

There is a minimal security validation in this module. Use it at your own risk. I suggest using https://github.com/dgrijalva/jwt-go for secure validation of your token.

Documentation

Overview

This is a simple JWT parser that converts any token to its appropriate string map interface.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ClientIDMismatchError = errors.New("client ID does not match token aud")

ClientIDMismatchError occurs when the token "aud" and Token.ClientID doesn't match

View Source
var TokenExpiredError = errors.New("access token is expired")

TokenExpiredError occurs when the token is expired

Functions

func InTimeSpan

func InTimeSpan(start, end, check time.Time) bool

InTimeSpan checks if two time stamps are in a given span

func UnixTimeToTime

func UnixTimeToTime(s int64) time.Time

UnixTimeToTime converts Unix time stamp to time.Time

Types

type Token

type Token struct {

	// JWT AccessToken
	AccessToken string `json:"access_token"`

	// JWT RefreshToken
	RefreshToken string `json:"refresh_token"`

	// JWT IdToken
	IdToken string `json:"id_token"`

	// ClientID of the provider
	ClientID string `json:"client_id"`
}

Token after an authentication is done

func ParseAccessToken

func ParseAccessToken(t, clientId string) Token

ParseAccessToken parses access token with client ID to return Token

Example
u := "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTQ1ODY5ODIsIm5iZiI6MTYxNDU4MzM4MiwidmVyIjoiMS4wIiwiaXNzIjoiaHR0cHM6Ly9nb2xsYWhhbGxpYXV0aC5iMmNsb2dpbi5jb20vNDkyMDY1NmQtMzZlMC00YjM1LTg5NGQtMGEwYTE0YmVjOGIzL3YyLjAvIiwic3ViIjoiNzYyNWY0MDktMWZhNy00ZDc4LTkzYjAtNWMzNDAyNTVjZGYyIiwiYXVkIjoiMTIzNDU2Nzg5Iiwibm9uY2UiOiJkZWZhdWx0Tm9uY2UiLCJpYXQiOjE2MTQ1ODMzODIsImF1dGhfdGltZSI6MTYxNDU4MzM4Miwib2lkIjoic29tZS1vaWQiLCJnaXZlbl9uYW1lIjoiQWtzaGF5IiwiZmFtaWx5X25hbWUiOiJHb2xsYWhhbGxpIiwidGZwIjoiQjJDXzFfU2lnblVwU2lnbkluRmxvdyJ9.CNbe3qUhpUR7U51e9k1miAdQP3ZEbCDip67MlvgTpFsFmB3nbjp7wi8e-66cPoS9z_hmQP3wLc5I8KE-b4_cFCzHZkhuQPA0Mhi9mBAZ_tAPSXNaDeiX1FEalsiH_sWuHWnojxSwlSxeKL9Tlh_0u5vXaABILdDeRWOTBJDHZ5I2BgIk8J_hI-fXDvBb0wfjI4mQe7lQqDZLVos4mlA1Uhpz2wN7Lorc31PZo02STbk5S1j1BMk4eQUS_OHTKZfAlUmfV9giWEMr7qk21eSy_HlybVKQgCB9Cde_rmNWJETyw6X712QGYWaDkDrwocQ99wR6rALWVkkTOhmL6CRT5Q"
c := "123456789"
var data providers.AzureB2C
token := ParseAccessToken(u, c)
err := token.UnmarshalAccessToken(&data)
if err != nil {
	panic(err)
}
fmt.Printf("%#v\n", data)
Output:

providers.AzureB2C{Exp:1614586982, Nbf:1614583382, Ver:"1.0", Iss:"https://gollahalliauth.b2clogin.com/4920656d-36e0-4b35-894d-0a0a14bec8b3/v2.0/", Sub:"7625f409-1fa7-4d78-93b0-5c340255cdf2", Aud:"123456789", Nonce:"defaultNonce", Iat:1614583382, AuthTime:1614583382, Oid:"some-oid", GivenName:"Akshay", FamilyName:"Gollahalli", Tfp:"B2C_1_SignUpSignInFlow", CHash:"", AtHash:"", Scp:"", IdpAccessToken:"", Idp:""}

func ParseIdToken

func ParseIdToken(t string) Token

ParseIdToken parses ID token to return Token

func ParseRefreshToken

func ParseRefreshToken(t string) Token

ParseRefreshToken parses refresh token to return Token

func ParseURL

func ParseURL(u, clientID string) (Token, error)

ParseURL gets the query from the URL and returns Token

Example

Simple way to parse a queried URL

u := "https://jwt.ms/?state=d61b0af6-4704-4070-b854-ee5feab01bf2&access_token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTQ1ODY5ODIsIm5iZiI6MTYxNDU4MzM4MiwidmVyIjoiMS4wIiwiaXNzIjoiaHR0cHM6Ly9nb2xsYWhhbGxpYXV0aC5iMmNsb2dpbi5jb20vNDkyMDY1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QP3ZEbCDip67MlvgTpFsFmB3nbjp7wi8e-66cPoS9z_hmQP3wLc5I8KE-b4_cFCzHZkhuQPA0Mhi9mBAZ_tAPSXNaDeiX1FEalsiH_sWuHWnojxSwlSxeKL9Tlh_0u5vXaABILdDeRWOTBJDHZ5I2BgIk8J_hI-fXDvBb0wfjI4mQe7lQqDZLVos4mlA1Uhpz2wN7Lorc31PZo02STbk5S1j1BMk4eQUS_OHTKZfAlUmfV9giWEMr7qk21eSy_HlybVKQgCB9Cde_rmNWJETyw6X712QGYWaDkDrwocQ99wR6rALWVkkTOhmL6CRT5Q"
c := "123456789"
var data providers.AzureB2C
token, err := ParseURL(u, c)
if err != nil {
	panic(err)
}
err = token.UnmarshalAccessToken(&data)
if err != nil {
	panic(err)
}
fmt.Printf("%#v\n", data)
Output:

providers.AzureB2C{Exp:1614586982, Nbf:1614583382, Ver:"1.0", Iss:"https://gollahalliauth.b2clogin.com/4920656d-36e0-4b35-894d-0a0a14bec8b3/v2.0/", Sub:"7625f409-1fa7-4d78-93b0-5c340255cdf2", Aud:"123456789", Nonce:"defaultNonce", Iat:1614583382, AuthTime:1614583382, Oid:"some-oid", GivenName:"Akshay", FamilyName:"Gollahalli", Tfp:"B2C_1_SignUpSignInFlow", CHash:"", AtHash:"", Scp:"", IdpAccessToken:"", Idp:""}

func (*Token) DecodeToken

func (t *Token) DecodeToken(token string) ([]byte, error)

func (*Token) UnmarshalAccessToken

func (t *Token) UnmarshalAccessToken(v interface{}) error

UnmarshalAccessToken converts Token.AccessToken to string map

func (*Token) UnmarshalIdToken

func (t *Token) UnmarshalIdToken(v interface{}) error

UnmarshalIdToken converts Token.IdToken to string map

func (*Token) UnmarshalRefreshToken

func (t *Token) UnmarshalRefreshToken(v interface{}) error

UnmarshalRefreshToken converts Token.RefreshToken to string map

func (*Token) Valid

func (t *Token) Valid() (bool, error)

Valid validates Token.AccessToken

Directories

Path Synopsis
These are the commonly available Oauth2 providers.
These are the commonly available Oauth2 providers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL