MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestAuthenticateTrustedJWT

Function TestAuthenticateTrustedJWT

auth/auth_test.go:796–1214  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

794}
795
796func TestAuthenticateTrustedJWT(t *testing.T) {
797 base.SetUpTestLogging(t, base.LevelDebug, base.KeyAuth, base.KeyAccess, base.KeyHTTP)
798 ctx := base.TestCtx(t)
799 testBucket := base.GetTestBucket(t)
800 defer testBucket.Close(ctx)
801
802 dataStore := testBucket.GetSingleDataStore()
803
804 auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))
805
806 var callbackURLFunc OIDCCallbackURLFunc
807 callbackURL := base.Ptr("http://comcast:4984/_callback")
808 providerGoogle := oidcProviderForTest(t, &OIDCProvider{
809 Name: "Google",
810 JWTConfigCommon: JWTConfigCommon{
811 ClientID: base.Ptr("aud1"),
812 Issuer: issuerGoogleAccounts,
813 },
814 CallbackURL: callbackURL,
815 })
816
817 // Make an RSA signer for signing tokens
818 signer, err := getRSASigner()
819 require.NoError(t, err, "Failed to create RSA signer")
820
821 t.Run("malformed token with bad header no payload", func(t *testing.T) {
822 user, _, expiry, err := auth.AuthenticateTrustedJWT("DmBb9C5", providerGoogle, callbackURLFunc)
823 assert.Error(t, err, "Error parsing malformed token")
824 assert.Nil(t, user, "User shouldn't be created or retrieved")
825 assert.Equal(t, time.Time{}, expiry, "Expiry should be zero time instant")
826 })
827
828 t.Run("malformed token with bad header bad payload", func(t *testing.T) {
829 user, _, expiry, err := auth.AuthenticateTrustedJWT("DmBb9C5.C#m7G#7", providerGoogle, callbackURLFunc)
830 assert.Error(t, err, "Error parsing malformed token")
831 assert.Nil(t, user, "User shouldn't be created or retrieved")
832 assert.Equal(t, time.Time{}, expiry, "Expiry should be zero time instant")
833 })
834
835 t.Run("malformed token with bad header bad base64 payload", func(t *testing.T) {
836 token := "DmBb9C5." + ToBase64String(`{"unknown":"value"}`)
837 user, _, expiry, err := auth.AuthenticateTrustedJWT(token, providerGoogle, callbackURLFunc)
838 assert.Error(t, err, "Error parsing malformed token")
839 assert.Nil(t, user, "User shouldn't be created or retrieved")
840 assert.Equal(t, time.Time{}, expiry, "Expiry should be zero time instant")
841 })
842
843 t.Run("token with issuer but no clientID config", func(t *testing.T) {
844 builder := jwt.Signed(signer).Claims(jwt.Claims{Issuer: issuerGoogleAccounts})
845 token, err := builder.Serialize()
846 require.NoError(t, err, "Error serializing token using compact serialization format")
847 provider := oidcProviderForTest(t, &OIDCProvider{
848 Name: providerGoogle.Name,
849 JWTConfigCommon: JWTConfigCommon{
850 Issuer: issuerGoogleAccounts,
851 },
852 CallbackURL: providerGoogle.CallbackURL,
853 })

Callers

nothing calls this directly

Calls 15

SetUpTestLoggingFunction · 0.92
TestCtxFunction · 0.92
GetTestBucketFunction · 0.92
PtrFunction · 0.92
NewTestAuthenticatorFunction · 0.85
oidcProviderForTestFunction · 0.85
getRSASignerFunction · 0.85
ToBase64StringFunction · 0.85
getJWTUsernameFunction · 0.85
InitUserPrefixMethod · 0.80

Tested by

no test coverage detected