Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the right base64 decoder as a JWT is a tuple of URL-safe base64 encoded strings #97

Merged
merged 2 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"testing"

"golang.org/x/oauth2"

"github.com/manicminer/hamilton/auth"
"github.com/manicminer/hamilton/environments"
"github.com/manicminer/hamilton/internal/test"
Expand All @@ -23,15 +25,16 @@ var (
)

func TestClientCertificateAuthorizerV1(t *testing.T) {
testClientCertificateAuthorizer(t, auth.TokenVersion1)
ctx := context.Background()
testClientCertificateAuthorizer(ctx, t, auth.TokenVersion1)
}

func TestClientCertificateAuthorizerV2(t *testing.T) {
testClientCertificateAuthorizer(t, auth.TokenVersion2)
ctx := context.Background()
testClientCertificateAuthorizer(ctx, t, auth.TokenVersion2)
}

func testClientCertificateAuthorizer(t *testing.T, tokenVersion auth.TokenVersion) {
ctx := context.Background()
func testClientCertificateAuthorizer(ctx context.Context, t *testing.T, tokenVersion auth.TokenVersion) (token *oauth2.Token) {
pfx := utils.Base64DecodeCertificate(clientCertificate)
auth, err := auth.NewClientCertificateAuthorizer(ctx, environments.Global, auth.MsGraph, tokenVersion, tenantId, clientId, pfx, clientCertificatePath, clientCertPassword)
if err != nil {
Expand All @@ -40,7 +43,7 @@ func testClientCertificateAuthorizer(t *testing.T, tokenVersion auth.TokenVersio
if auth == nil {
t.Fatal("auth is nil, expected Authorizer")
}
token, err := auth.Token()
token, err = auth.Token()
if err != nil {
t.Fatalf("auth.Token(): %v", err)
}
Expand All @@ -50,26 +53,28 @@ func testClientCertificateAuthorizer(t *testing.T, tokenVersion auth.TokenVersio
if token.AccessToken == "" {
t.Fatal("token.AccessToken was empty")
}
return
}

func TestClientSecretAuthorizerV1(t *testing.T) {
testClientSecretAuthorizer(t, auth.TokenVersion1)
ctx := context.Background()
testClientSecretAuthorizer(ctx, t, auth.TokenVersion1)
}

func TestClientSecretAuthorizerV2(t *testing.T) {
testClientSecretAuthorizer(t, auth.TokenVersion2)
ctx := context.Background()
testClientSecretAuthorizer(ctx, t, auth.TokenVersion2)
}

func testClientSecretAuthorizer(t *testing.T, tokenVersion auth.TokenVersion) {
ctx := context.Background()
func testClientSecretAuthorizer(ctx context.Context, t *testing.T, tokenVersion auth.TokenVersion) (token *oauth2.Token) {
auth, err := auth.NewClientSecretAuthorizer(ctx, environments.Global, auth.MsGraph, tokenVersion, tenantId, clientId, clientSecret)
if err != nil {
t.Fatalf("NewClientSecretAuthorizer(): %v", err)
}
if auth == nil {
t.Fatal("auth is nil, expected Authorizer")
}
token, err := auth.Token()
token, err = auth.Token()
if err != nil {
t.Fatalf("auth.Token(): %v", err)
}
Expand All @@ -79,18 +84,23 @@ func testClientSecretAuthorizer(t *testing.T, tokenVersion auth.TokenVersion) {
if token.AccessToken == "" {
t.Fatalf("token.AccessToken was empty")
}
return
}

func TestAzureCliAuthorizer(t *testing.T) {
ctx := context.Background()
testAzureCliAuthorizer(ctx, t)
}

func testAzureCliAuthorizer(ctx context.Context, t *testing.T) (token *oauth2.Token) {
auth, err := auth.NewAzureCliAuthorizer(ctx, auth.MsGraph, tenantId)
if err != nil {
t.Fatalf("NewAzureCliAuthorizer(): %v", err)
}
if auth == nil {
t.Fatal("auth is nil, expected Authorizer")
}
token, err := auth.Token()
token, err = auth.Token()
if err != nil {
t.Fatalf("auth.Token(): %v", err)
}
Expand All @@ -100,6 +110,7 @@ func TestAzureCliAuthorizer(t *testing.T) {
if token.AccessToken == "" {
t.Fatalf("token.AccessToken was empty")
}
return
}

func TestMsiAuthorizer(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion auth/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func ParseClaims(token *oauth2.Token) (claims Claims, err error) {
return
}
jwt := strings.Split(token.AccessToken, ".")
payload, err := base64.RawStdEncoding.DecodeString(jwt[1])
payload, err := base64.RawURLEncoding.DecodeString(jwt[1])
if err != nil {
return
}
Expand Down
59 changes: 59 additions & 0 deletions auth/claims_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package auth_test

import (
"context"
"testing"

"github.com/manicminer/hamilton/auth"
)

func TestParseClaims_azureCli(t *testing.T) {
ctx := context.Background()
token := testAzureCliAuthorizer(ctx, t)
claims, err := auth.ParseClaims(token)
if err != nil {
t.Fatal(err)
}
checkClaims(t, claims)
}

func TestParseClaims_clientCertificate(t *testing.T) {
ctx := context.Background()
token := testClientCertificateAuthorizer(ctx, t, auth.TokenVersion2)
claims, err := auth.ParseClaims(token)
if err != nil {
t.Fatal(err)
}
checkClaims(t, claims)
}

func TestParseClaims_clientSecret(t *testing.T) {
ctx := context.Background()
token := testClientSecretAuthorizer(ctx, t, auth.TokenVersion2)
claims, err := auth.ParseClaims(token)
if err != nil {
t.Fatal(err)
}
checkClaims(t, claims)
}

func checkClaims(t *testing.T, claims auth.Claims) {
if claims.AppId == "" {
t.Fatal("claims.AppId was empty")
}
if claims.Audience == "" {
t.Fatal("claims.Audience was empty")
}
if claims.Issuer == "" {
t.Fatal("claims.Issuer was empty")
}
if len(claims.Roles) == 0 && claims.Scopes == "" {
t.Fatal("claims.Roles and claims.Scopes were empty")
}
if claims.Subject == "" {
t.Fatal("claims.Subject was empty")
}
if claims.TenantId == "" {
t.Fatal("claims.TenantId was empty")
}
}