-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from okta/pkce_utility
PKCE utility
- Loading branch information
Showing
15 changed files
with
313 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# This GitHub action can publish assets for release when a tag is created. | ||
# Currently its setup to run on any tag that matches the pattern "v*" (ie. v0.1.0). | ||
# | ||
# This uses an action (hashicorp/ghaction-import-gpg) that assumes you set your | ||
# private key in the `GPG_PRIVATE_KEY` secret and passphrase in the `PASSPHRASE` | ||
# secret. If you would rather own your own GPG handling, please fork this action | ||
# or use an alternative one for key handling. | ||
# | ||
# You will need to pass the `--batch` flag to `gpg` in your signing step | ||
# in `goreleaser` to indicate this is being used in a non-interactive mode. | ||
# | ||
name: release | ||
on: | ||
push: | ||
branches: [ master ] | ||
tags: | ||
- 'v*' | ||
jobs: | ||
goreleaser: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- | ||
name: Checkout | ||
uses: actions/[email protected] | ||
- | ||
name: Unshallow | ||
run: git fetch --prune --unshallow | ||
- | ||
name: Set up Go | ||
uses: actions/[email protected] | ||
with: | ||
go-version: 1.17 | ||
- | ||
name: Run GoReleaser | ||
uses: goreleaser/[email protected] | ||
with: | ||
version: latest | ||
args: release --rm-dist | ||
env: | ||
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }} | ||
# GitHub sets this automatically | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Copyright (c) 2015-2018 Okta Inc. | ||
Copyright (c) 2015-Present Okta Inc. | ||
|
||
Apache License | ||
Version 2.0, January 2004 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/******************************************************************************* | ||
* Copyright 2022 - Present Okta, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
******************************************************************************/ | ||
|
||
// based on https://datatracker.ietf.org/doc/html/rfc7636 | ||
package utils | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"fmt" | ||
"math/rand" | ||
"strings" | ||
) | ||
|
||
const ( | ||
MinLength = 32 | ||
MaxLength = 96 | ||
) | ||
|
||
type PKCECodeVerifier struct { | ||
CodeVerifier string | ||
} | ||
|
||
func (v *PKCECodeVerifier) String() string { | ||
return v.CodeVerifier | ||
} | ||
|
||
// CodeChallengePlain generates a plain code challenge from a code verifier | ||
func (v *PKCECodeVerifier) CodeChallengePlain() string { | ||
return v.CodeVerifier | ||
} | ||
|
||
// CodeChallengeS256 generates a Sha256 code challenge from a code verifier | ||
func (v *PKCECodeVerifier) CodeChallengeS256() string { | ||
h := sha256.New() | ||
h.Write([]byte(v.CodeVerifier)) | ||
return encode(h.Sum(nil)) | ||
} | ||
|
||
// GenerateCodeVerifier generates a code verifier with the minimum length | ||
func GenerateCodeVerifier() (*PKCECodeVerifier, error) { | ||
return GenerateCodeVerifierWithLength(MinLength) | ||
} | ||
|
||
// GenerateCodeVerifierWithLength generates a code verifier with the specified length | ||
func GenerateCodeVerifierWithLength(length int) (*PKCECodeVerifier, error) { | ||
if length < MinLength || length > MaxLength { | ||
return nil, fmt.Errorf("invalid length: %v", length) | ||
} | ||
// create random bytes | ||
b, err := bytes(length) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &PKCECodeVerifier{ | ||
CodeVerifier: encode(b), | ||
}, nil | ||
} | ||
|
||
// bytes generates n random bytes | ||
func bytes(n int) ([]byte, error) { | ||
b := make([]byte, n) | ||
_, err := rand.Read(b) | ||
return b, err | ||
} | ||
|
||
// encode encodes a byte array to a base64 string with no padding | ||
func encode(b []byte) string { | ||
encoded := base64.StdEncoding.EncodeToString(b) | ||
encoded = strings.Replace(encoded, "+", "-", -1) | ||
encoded = strings.Replace(encoded, "/", "_", -1) | ||
encoded = strings.Replace(encoded, "=", "", -1) | ||
return encoded | ||
} |
Oops, something went wrong.