Skip to content

The package to implement authentication (jwt-based) and authorization (in progress...) in Golang.

License

Notifications You must be signed in to change notification settings

Rayato159/rainbowhatto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌈 Rainbow Hatto

Rainbow Hatto is the authentication and authorization package for Golang.

The authentication is based on Jwt (Json Web Token) and the authorization methodology is based on role-based access control (RBAC)

Content

Installation

go get github.com/Rayato159/rainbowhatto

Function

func BuildToken(alg src.SignAlgorithm, cfg Config) src.IToken {...}
func ReverseHMACToken(token string, secret string) (*Claims, error) {...}
func ReverseRSAToken(token string, path string) (*Claims, error) {...}
func RefreshToken(alg src.SignAlgorithm, token string, cfg Config) (src.IToken, error) {...}

Type

SignAlgorithm

Just call a below function to get a SignAlgorithm type

func HMAC() src.SignAlgorithm {...} // return HMAC type
func RSA() src.SignAlgorithm {...} // return RSA type

Claims

type Claims struct {
	*src.NewClaims `json:"claims"`
}

type NewClaims struct {
	Claims any `json:"claims"`
	jwt.RegisteredClaims
}

Detail of registered claims

ID:        xid,
Issuer:    "rainbowhatto",
Subject:   "rainbowtoken",
Audience:  ["human"],
ExpiresAt: time exp,
NotBefore: time now,
IssuedAt:  time now,

Quickstart

HMAC token (Symmetric key)

Sign Token

func main() {
	token, err := rainbowhatto.BuildToken(rainbowhatto.HMAC(), rainbowhatto.Config{
		ExpiresAt: 86400,
		HMAC: &rainbowhatto.HMACConfig{
			Secret: "super-secret",
		},
		Claims: claims{
			Id:   "abdcefg1234",
			Name: "rainbow",
		},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(token.SignToken())
}

Parse Token

func main() {
    token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGFpbXMiOnsiSWQiOiJhYmRjZWZnMTIzNCIsIk5hbWUiOiJyYWluYm93In0sImlzcyI6InJhaW5ib3doYXR0byIsInN1YiI6InJhaW5ib3d0b2tlbiIsImF1ZCI6WyJodW1hbiJdLCJleHAiOjE2NzY4MjcxMzIsIm5iZiI6MTY3Njc0MDczMiwiaWF0IjoxNjc2NzQwNzMyLCJqdGkiOiJjZm9nZ3Y2bmRyYzBibjRyOGQ4MCJ9.lzBu_zRgtc0oTqkZyjatJu7u8PGeBXALcICdTf7zUcs"
    claims, err := rainbowhatto.ReverseHMACToken(token, "super-secret")
    if err != nil {
        panic(err)
    }
    fmt.Println(claims)
}

Refresh Token

func main() {
    oldToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGFpbXMiOnsiSWQiOiJhYmRjZWZnMTIzNCIsIk5hbWUiOiJyYWluYm93In0sImlzcyI6InJhaW5ib3doYXR0byIsInN1YiI6InJhaW5ib3d0b2tlbiIsImF1ZCI6WyJodW1hbiJdLCJleHAiOjE2NzY4MjcxMzIsIm5iZiI6MTY3Njc0MDczMiwiaWF0IjoxNjc2NzQwNzMyLCJqdGkiOiJjZm9nZ3Y2bmRyYzBibjRyOGQ4MCJ9.lzBu_zRgtc0oTqkZyjatJu7u8PGeBXALcICdTf7zUcs"

    newToken, err := rainbowhatto.RefreshToken(rainbowhatto.HMAC(), oldToken, rainbowhatto.Config{
		ExpiresAt: 0,
		HMAC: &rainbowhatto.HMACConfig{
			Secret: "super-secret",
		},
		Claims: claims{
			Id:   "abdcefg1234",
			Name: "rainbow",
		},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(newToken.SignToken())
}

RSA token (asymmetric key)

First Generate a .pem key by the following command as below

# Gen a private key (the number is just a bytes)
openssl genrsa -out <file_name>.pem 2048

# Get a public key
openssl rsa -in <file_name>.pem -pubout -out public.pem

Sign Token

func main() {
	token, err := rainbowhatto.BuildToken(rainbowhatto.RSA(), rainbowhatto.Config{
		ExpiresAt: 86400,
		RSA: &rainbowhatto.RSAConfig{
			PrivateKey: "./private_key.pem",
		},
		Claims: claims{
			Id:   "abdcefg1234",
			Name: "rainbow",
		},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(token.SignToken())
}

Parse Token

func main() {
    token := "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGFpbXMiOnsiSWQiOiJhYmRjZWZnMTIzNCIsIk5hbWUiOiJyYWluYm93In0sImlzcyI6InJhaW5ib3doYXR0byIsInN1YiI6InJhaW5ib3d0b2tlbiIsImF1ZCI6WyJodW1hbiJdLCJleHAiOjE2NzY4MjcxNTIsIm5iZiI6MTY3Njc0MDc1MiwiaWF0IjoxNjc2NzQwNzUyLCJqdGkiOiJjZm9naDQ2bmRyYzRwODc0MHBjZyJ9.NSB3DoBjw4XNkiB8_Cnw29qioVp1Y9nRBj5To-k-_yldx74hquGEvni7ZyHio_eAoPRAbi8EdZNNtLyt0wSl3bLvzgsl4b5fvHnVfcp55i9lyUH0odDHnNXq7fWOcNqH4QaMVF2LcJ66AffjDgiePbR7ob8YyovgMDYjU4x73wkyrzNqAJBugbjgBX9g1wd-aGo9N1i0sYas6YBMRbQAhl4XrtVpZj-YQkHePYYrU6Xt6DiE5vhtAuiDRqD4B9gXOStHV6VtLVnjAFJSFidYAXjV0GKzdaOl84yddNL2ZSwFf6JcD4AJ7AGuIlXmA7EC5yC5pwKjVNcFopVZjUKjyA"
    claims, err := rainbowhatto.ReverseHMACToken(token, "./public.pem") // Public key path
    if err != nil {
        panic(err)
    }
    fmt.Println(claims)
}

Refresh Token

func main() {
    oldToken := "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGFpbXMiOnsiSWQiOiJhYmRjZWZnMTIzNCIsIk5hbWUiOiJyYWluYm93In0sImlzcyI6InJhaW5ib3doYXR0byIsInN1YiI6InJhaW5ib3d0b2tlbiIsImF1ZCI6WyJodW1hbiJdLCJleHAiOjE2NzY4MjcxNTIsIm5iZiI6MTY3Njc0MDc1MiwiaWF0IjoxNjc2NzQwNzUyLCJqdGkiOiJjZm9naDQ2bmRyYzRwODc0MHBjZyJ9.NSB3DoBjw4XNkiB8_Cnw29qioVp1Y9nRBj5To-k-_yldx74hquGEvni7ZyHio_eAoPRAbi8EdZNNtLyt0wSl3bLvzgsl4b5fvHnVfcp55i9lyUH0odDHnNXq7fWOcNqH4QaMVF2LcJ66AffjDgiePbR7ob8YyovgMDYjU4x73wkyrzNqAJBugbjgBX9g1wd-aGo9N1i0sYas6YBMRbQAhl4XrtVpZj-YQkHePYYrU6Xt6DiE5vhtAuiDRqD4B9gXOStHV6VtLVnjAFJSFidYAXjV0GKzdaOl84yddNL2ZSwFf6JcD4AJ7AGuIlXmA7EC5yC5pwKjVNcFopVZjUKjyA"

    newToken, err := rainbowhatto.RefreshToken(rainbowhatto.RSA(), oldToken, rainbowhatto.Config{
		RSA: &rainbowhatto.RSAConfig{
			PrivateKey: "./private_key.pem",
			PublicKey:  "./public_key.pem",
		},
		Claims: claims{
			Id:   "abdcefg1234",
			Name: "rainbow",
		},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(newToken.SignToken())
}

About

The package to implement authentication (jwt-based) and authorization (in progress...) in Golang.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages