(Feat): Initial Commit
This commit is contained in:
35
internal/util/oauth2/pkce.go
Normal file
35
internal/util/oauth2/pkce.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package oauth2
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
|
||||
"allaboutapps.dev/aw/go-starter/internal/util"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultVerifierLength = 128
|
||||
)
|
||||
|
||||
func GetPKCECodeVerifier() (string, error) {
|
||||
// for details regarding possible characters in verifier, see:
|
||||
// https://tools.ietf.org/html/rfc7636#section-4.1
|
||||
verifier, err := util.GenerateRandomString(defaultVerifierLength, []util.CharRange{util.CharRangeNumeric, util.CharRangeAlphaLowerCase, util.CharRangeAlphaUpperCase}, "-._~")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to generate random string: %w", err)
|
||||
}
|
||||
|
||||
return verifier, nil
|
||||
}
|
||||
|
||||
func GetPKCECodeChallengeS256(verifier string) string {
|
||||
// for details regarding transformation of verifier to challenge see:
|
||||
// https://tools.ietf.org/html/rfc7636#section-4.2
|
||||
// base64 encoding must be unpadded, URL encoding:
|
||||
// https://tools.ietf.org/html/rfc7636#page-17
|
||||
sum := sha256.Sum256([]byte(verifier))
|
||||
b64 := base64.RawURLEncoding.EncodeToString(sum[:])
|
||||
|
||||
return b64
|
||||
}
|
||||
16
internal/util/oauth2/pkce_test.go
Normal file
16
internal/util/oauth2/pkce_test.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package oauth2_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"allaboutapps.dev/aw/go-starter/internal/util/oauth2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetPKCECodeChallengeS256(t *testing.T) {
|
||||
verifier := "U7MEZRmshzwIHRIGvF5iy6FLKgTtUHV0Vb0Hpczh6jJ_XZKcQIurow2LvsjG6hx2k57s9Pz8UmCZTvazosnniTM-z6EC.skJlQMGA~8ue3LMiOWdFYTfsLdX8GKol285"
|
||||
expected := "Jg697bAjhzV1upYvV9R04784OFNVRAZh2IjeFlMJ8bE"
|
||||
|
||||
challenge := oauth2.GetPKCECodeChallengeS256(verifier)
|
||||
assert.Equal(t, expected, challenge)
|
||||
}
|
||||
Reference in New Issue
Block a user