forked from sigstore/sigstore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
oauthflow: Add SubjectFromUnverifiedToken (sigstore#1826)
This commit adds a SubjectFromUnverifiedToken function to accompany SubjectFromToken. Unlike SubjectFromToken which accepts a token that is verified against the issuing IDP, this function operates on the raw bytes of the token. This is useful for clients or libraries that need to know the subject of the token to sign proof of key possession over when requesting a certificate from Fulcio, but do not want to verify the token's signature as Fulcio will do so anyway. Signed-off-by: Aditya Sirish <[email protected]>
- Loading branch information
1 parent
b27128f
commit 4c750b7
Showing
2 changed files
with
80 additions
and
0 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
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 |
---|---|---|
|
@@ -267,3 +267,73 @@ func TestSubjectFromToken(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestSubjectFromUnverifiedToken(t *testing.T) { | ||
tests := map[string]struct { | ||
Subject string | ||
Email string | ||
EmailVerified interface{} | ||
ExpectedSubject string | ||
WantErr bool | ||
}{ | ||
`Email as subject`: { | ||
Email: "[email protected]", | ||
EmailVerified: true, | ||
Subject: "foobar", | ||
ExpectedSubject: "[email protected]", | ||
WantErr: false, | ||
}, | ||
`Subject as subject`: { | ||
Subject: "foobar", | ||
ExpectedSubject: "foobar", | ||
WantErr: false, | ||
}, | ||
`no email or subject`: { | ||
WantErr: true, | ||
}, | ||
`String email_verified value`: { | ||
Email: "[email protected]", | ||
EmailVerified: "true", | ||
ExpectedSubject: "[email protected]", | ||
WantErr: false, | ||
}, | ||
`Email not verified`: { | ||
Email: "[email protected]", | ||
EmailVerified: false, | ||
WantErr: true, | ||
}, | ||
`invalid email_verified property`: { | ||
Email: "[email protected]", | ||
EmailVerified: "foo", | ||
WantErr: true, | ||
}, | ||
} | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
inputClaims := map[string]interface{}{ | ||
"email": test.Email, | ||
"sub": test.Subject, | ||
} | ||
|
||
if test.EmailVerified != nil { | ||
inputClaims["email_verified"] = test.EmailVerified | ||
} | ||
|
||
token, err := json.Marshal(inputClaims) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
subject, err := SubjectFromUnverifiedToken(token) | ||
if err != nil { | ||
if !test.WantErr { | ||
t.Fatal("didn't expect error", err) | ||
} | ||
} | ||
|
||
if subject != test.ExpectedSubject { | ||
t.Errorf("got %v subject and expected %v", subject, test.Subject) | ||
} | ||
}) | ||
} | ||
} |