https://github.com/grokify/go-pkce
Go library for OAuth 2.0 PKCE.
https://github.com/grokify/go-pkce
Last synced: about 2 months ago
JSON representation
Go library for OAuth 2.0 PKCE.
- Host: GitHub
- URL: https://github.com/grokify/go-pkce
- Owner: grokify
- License: mit
- Created: 2021-11-10T07:52:22.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-03-17T09:28:40.000Z (3 months ago)
- Last Synced: 2025-05-07T18:07:48.336Z (about 2 months ago)
- Language: Go
- Homepage:
- Size: 35.2 KB
- Stars: 12
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PKCE for Go
[![CI Status][build-status-svg]][build-status-url]
[![Lint Status][lint-status-svg]][lint-status-url]
[![Go Report Card][goreport-svg]][goreport-url]
[![Code Coverage][codecov-svg]][codecov-url]
[![Docs][docs-godoc-svg]][docs-godoc-url]
[![License][license-svg]][license-url][build-status-svg]: https://github.com/grokify/go-pkce/actions/workflows/ci.yaml/badge.svg?branch=master
[build-status-url]: https://github.com/grokify/go-pkce/actions/workflows/ci.yaml
[lint-status-svg]: https://github.com/grokify/go-pkce/actions/workflows/lint.yaml/badge.svg?branch=master
[lint-status-url]: https://github.com/grokify/go-pkce/actions/workflows/lint.yaml
[goreport-svg]: https://goreportcard.com/badge/github.com/grokify/go-pkce
[goreport-url]: https://goreportcard.com/report/github.com/grokify/go-pkce
[codecov-svg]: https://codecov.io/gh/grokify/go-pkce/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/grokify/go-pkce
[docs-godoc-svg]: https://pkg.go.dev/badge/github.com/grokify/go-pkce
[docs-godoc-url]: https://pkg.go.dev/github.com/grokify/go-pkce
[license-svg]: https://img.shields.io/badge/license-MIT-blue.svg
[license-url]: https://github.com/grokify/go-pkce/blob/master/LICENSE`go-pkce` package contains an implementation for OAuth 2.0 PKCE spec, [IETF RFC 7636](https://datatracker.ietf.org/doc/html/rfc7636).
## 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
```go
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`
```go
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
* [`github.com/nirasan/go-oauth-pkce-code-verifier`](https://github.com/nirasan/go-oauth-pkce-code-verifier)