pkce

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2022 License: MIT Imports: 6 Imported by: 0

README

Proof Key for Code Exchange (PKCE)

Go GoDoc codecov

Generate PKCE verify and challenge code.

Instillation

go get github.com/akshaybabloo/pkce/v2

Usage

There are two ways of doing this

Your own string

You can put in your own string, example:

package main

import (
	"fmt"

	"github.com/akshaybabloo/pkce/v2"
)

func main() {
	p := pkce.Pkce{
		RandomString: "45d7820e694481f399e7fb9c444f0cb63301a7254d1401443835d9af2c9a6a5ec5b243c3470feb945336025964ef05c8d2f0e44baf76762ba6136914",
	}

	fmt.Println(p.VerifyCode())  // This is optional
	// Output: 45d7820e694481f399e7fb9c444f0cb63301a7254d1401443835d9af2c9a6a5ec5b243c3470feb945336025964ef05c8d2f0e44baf76762ba6136914
	
	fmt.Println(p.ChallengeCode())
	// Output: iQoF8w9kq5RnuMdisRXypyOoMCF7FGz-ro7dwHjC28U
}

When you enter your own string, calling p.VerifyCode() is optional

Let us generate one for you

You can let the module generate a random string for you, but the length should be more than or equal to 43 and lest and or equal to 128.

package main

import (
	"fmt"

	"github.com/akshaybabloo/pkce/v2"
)

func main() {
	p := pkce.Pkce{
		Length: 128,
	}

	fmt.Println(p.VerifyCode()) // This is optional
	// Output: 45d7820e694481f399e7fb9c444f0cb63301a7254d1401443835d9af2c9a6a5ec5b243c3470feb945336025964ef05c8d2f0e44baf76762ba6136914
	
	fmt.Println(p.ChallengeCode())
	// Output: iQoF8w9kq5RnuMdisRXypyOoMCF7FGz-ro7dwHjC28U
}

Note: Calling p.VerifyCode() optional, but calling it after p.ChallengeCode() will reset pkce.RandomString

Documentation

Overview

Proof Key for Code Exchange (PKCE) helps public clients to get access tokens and refresh tokens. PKCE is based on RFC7636, more on that can be found at https://tools.ietf.org/html/rfc7636 and https://auth0.com/docs/flows/authorization-code-flow-with-proof-key-for-code-exchange-pkce.

Index

Examples

Constants

View Source
const LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~"

LETTERS according to https://tools.ietf.org/html/rfc7636#section-4.1

Variables

This section is empty.

Functions

func GenerateRandomString

func GenerateRandomString(l int) string

GenerateRandomString returns a random string of a given length

Types

type Pkce

type Pkce struct {

	// Optional, RandomString, this will be converted to Pkce.ChallengeCode
	RandomString string

	// Optional, Length of the Pkce.VerifyCode. When RandomString is provided, the Length is ignored.
	// The value should be minimum 43 and maximum 128.
	Length int
}

func (*Pkce) ChallengeCode

func (p *Pkce) ChallengeCode() (string, error)

ChallengeCode returns a challenge code as mentioned in https://tools.ietf.org/html/rfc7636#section-4.2. The code is based on

code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))
Example
p := Pkce{
	RandomString: "45d7820e694481f399e7fb9c444f0cb63301a7254d1401443835d9af2c9a6a5ec5b243c3470feb945336025964ef05c8d2f0e44baf76762ba6136914",
}
code, err := p.ChallengeCode()
if err != nil {
	panic(err)
}
fmt.Println(code)
Output:

iQoF8w9kq5RnuMdisRXypyOoMCF7FGz-ro7dwHjC28U
Example (GeneratedString)
p := Pkce{
	Length: 60,
}
code, err := p.VerifyCode()
if err != nil {
	panic(err)
}
challengeCode, err := p.ChallengeCode()
if err != nil {
	panic(err)
}
fmt.Println(code, challengeCode)
Output:

func (*Pkce) VerifyCode

func (p *Pkce) VerifyCode() (string, error)

When Pkce.RandomString is provided, VerifyCode is similar to it if not, VerifyCode returns a random string based on Pkce.Length.

Example
p := Pkce{
	RandomString: "ThisIsAVeryBiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiigRandomString",
}

code, err := p.VerifyCode()
if err != nil {
	panic(err)
}
fmt.Println(code)
Output:

ThisIsAVeryBiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiigRandomString
Example (GeneratedString)
p := Pkce{
	Length: 60,
}

code, err := p.VerifyCode()
if err != nil {
	panic(err)
}
fmt.Println(code)
Output:

Jump to

Keyboard shortcuts

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