pkce

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2023 License: MIT Imports: 4 Imported by: 11

README

PKCE for Go

Build Status Go Report Card Code Coverage Docs License

go-pkce package contains an implementation for OAuth 2.0 PKCE spec, IETF RFC 7636.

Installation

go get github.com/grokify/go-pkce

Or you can manually git clone the repository to $(go env GOPATH)/src/github.com/grokify/go-pkce.

Usage

import("github.com/grokify/go-pkce")

func main() {
  // Create a code_verifier with default 32 byte length.
  codeVerifier := pkce.NewCodeVerifier(-1)

  // Create a code_verifier with a custom length (32-96 bytes)
  codeVerifier, err := pkce.NewCodeVerifierWithLength(96)

  // Create a code_challenge using `S256`
  codeChallenge := pkce.CodeChallengeS256(codeVerifier)
}

Usage with oauth2

import(
  "context"

  "github.com/grokify/go-pkce"
  "golang.org/x/oauth2"
)

func main() {
  // Create a code_verifier with default 32 byte length.
  codeVerifier := pkce.NewCodeVerifier()

  // Create a code_challenge using `S256`
  codeChallenge := pkce.CodeChallengeS256(codeVerifier)

  // Create authorization_code URL using `oauth2.Config`
  authURL := oauth2Config.AuthCodeURL(
    "myState",
    oauth2.SetAuthURLParam(pkce.ParamCodeChallenge, codeChallenge),
    oauth2.SetAuthURLParam(pkce.ParamCodeChallengeMethod, pkce.MethodS256))

  // ... retrieve authorization_code ...

  // Exchange the authorization_code for a token with PKCE.
  token, err := oauth2Config.Exchange(
    context.Background(),
    "myCode",
    oauth2.SetAuthURLParam(pkce.ParamCodeVerifier, codeVerifier),
  )
}

Similar projects

Documentation

Index

Constants

View Source
const (
	LengthDefault = 32
	LengthMin     = 32
	LengthMax     = 96

	MethodPlain = "plain"
	MethodS256  = "S256"

	ParamCodeVerifier        = "code_verifier"
	ParamCodeChallenge       = "code_challenge"
	ParamCodeChallengeMethod = "code_challenge_method"
)

Variables

This section is empty.

Functions

func CodeChallengeS256

func CodeChallengeS256(v string) string

func NewCodeVerifier

func NewCodeVerifier(n int) (string, error)

func NewCodeVerifierBytes added in v0.2.1

func NewCodeVerifierBytes(b []byte) string

Types

This section is empty.

Jump to

Keyboard shortcuts

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