Files
probo/pkg/iam/oauth2/pkce_test.go
Ludovic Vielle 3ebb221a9b Add OAuth2 API scope registration and enforcement
Register v1 API scopes in coredata, advertise them in OIDC discovery
and protected-resource metadata, show them on the consent screen, and
enforce scope-to-action mapping in the IAM Authorizer before policy
evaluation.

Signed-off-by: Ludovic Vielle <ludovic@probo.com>
2026-06-18 19:07:25 +02:00

159 lines
3.1 KiB
Go

// Copyright (c) 2026 Probo Inc <hello@probo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package oauth2_test
import (
"crypto/sha256"
"encoding/base64"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.probo.inc/probo/pkg/coredata"
"go.probo.inc/probo/pkg/iam/oauth2"
)
func computeS256Challenge(verifier string) string {
h := sha256.Sum256([]byte(verifier))
return base64.RawURLEncoding.EncodeToString(h[:])
}
func TestValidateCodeChallenge(t *testing.T) {
t.Parallel()
verifier := "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
challenge := computeS256Challenge(verifier)
t.Run(
"valid s256",
func(t *testing.T) {
t.Parallel()
result := oauth2.ValidateCodeChallenge(
verifier,
challenge,
coredata.OAuth2CodeChallengeMethodS256,
)
require.True(t, result)
},
)
t.Run(
"wrong verifier",
func(t *testing.T) {
t.Parallel()
result := oauth2.ValidateCodeChallenge(
"wrong-verifier",
challenge,
coredata.OAuth2CodeChallengeMethodS256,
)
assert.False(t, result)
},
)
t.Run(
"wrong challenge",
func(t *testing.T) {
t.Parallel()
result := oauth2.ValidateCodeChallenge(
verifier,
"wrong-challenge",
coredata.OAuth2CodeChallengeMethodS256,
)
assert.False(t, result)
},
)
t.Run(
"unsupported method",
func(t *testing.T) {
t.Parallel()
result := oauth2.ValidateCodeChallenge(
verifier,
challenge,
coredata.OAuth2CodeChallengeMethod("plain"),
)
assert.False(t, result)
},
)
t.Run(
"empty method",
func(t *testing.T) {
t.Parallel()
result := oauth2.ValidateCodeChallenge(
verifier,
challenge,
coredata.OAuth2CodeChallengeMethod(""),
)
assert.False(t, result)
},
)
t.Run(
"empty verifier",
func(t *testing.T) {
t.Parallel()
result := oauth2.ValidateCodeChallenge(
"",
challenge,
coredata.OAuth2CodeChallengeMethodS256,
)
assert.False(t, result)
},
)
t.Run(
"empty challenge",
func(t *testing.T) {
t.Parallel()
result := oauth2.ValidateCodeChallenge(
verifier,
"",
coredata.OAuth2CodeChallengeMethodS256,
)
assert.False(t, result)
},
)
t.Run(
"both empty",
func(t *testing.T) {
t.Parallel()
result := oauth2.ValidateCodeChallenge(
"",
"",
coredata.OAuth2CodeChallengeMethodS256,
)
assert.False(t, result)
},
)
}