11package google_test
22
33import (
4+ "encoding/json"
45 "fmt"
56 "os"
67 "testing"
@@ -31,7 +32,7 @@ func Test_BeginAuth(t *testing.T) {
3132 a .Contains (s .AuthURL , "accounts.google.com/o/oauth2/auth" )
3233 a .Contains (s .AuthURL , fmt .Sprintf ("client_id=%s" , os .Getenv ("GOOGLE_KEY" )))
3334 a .Contains (s .AuthURL , "state=test_state" )
34- a .Contains (s .AuthURL , "scope=email" )
35+ a .Contains (s .AuthURL , "scope=openid+ email+profile " )
3536 a .Contains (s .AuthURL , "access_type=offline" )
3637}
3738
@@ -50,7 +51,7 @@ func Test_BeginAuthWithPrompt(t *testing.T) {
5051 a .Contains (s .AuthURL , "accounts.google.com/o/oauth2/auth" )
5152 a .Contains (s .AuthURL , fmt .Sprintf ("client_id=%s" , os .Getenv ("GOOGLE_KEY" )))
5253 a .Contains (s .AuthURL , "state=test_state" )
53- a .Contains (s .AuthURL , "scope=email" )
54+ a .Contains (s .AuthURL , "scope=openid+ email+profile " )
5455 a .Contains (s .AuthURL , "access_type=offline" )
5556 a .Contains (s .AuthURL , "prompt=test+prompts" )
5657}
@@ -70,7 +71,7 @@ func Test_BeginAuthWithHostedDomain(t *testing.T) {
7071 a .Contains (s .AuthURL , "accounts.google.com/o/oauth2/auth" )
7172 a .Contains (s .AuthURL , fmt .Sprintf ("client_id=%s" , os .Getenv ("GOOGLE_KEY" )))
7273 a .Contains (s .AuthURL , "state=test_state" )
73- a .Contains (s .AuthURL , "scope=email" )
74+ a .Contains (s .AuthURL , "scope=openid+ email+profile " )
7475 a .Contains (s .AuthURL , "access_type=offline" )
7576 a .Contains (s .AuthURL , "hd=example.com" )
7677}
@@ -90,7 +91,7 @@ func Test_BeginAuthWithLoginHint(t *testing.T) {
9091 a .Contains (s .AuthURL , "accounts.google.com/o/oauth2/auth" )
9192 a .Contains (s .AuthURL , fmt .Sprintf ("client_id=%s" , os .Getenv ("GOOGLE_KEY" )))
9293 a .Contains (s .AuthURL , "state=test_state" )
93- a .Contains (s .AuthURL , "scope=email" )
94+ a .Contains (s .AuthURL , "scope=openid+ email+profile " )
9495 a .Contains (s .AuthURL , "access_type=offline" )
9596 a .Contains (s .AuthURL , "login_hint=john%40example.com" )
9697}
@@ -115,6 +116,37 @@ func Test_SessionFromJSON(t *testing.T) {
115116 a .Equal (session .AccessToken , "1234567890" )
116117}
117118
119+ func Test_UserIDHandling (t * testing.T ) {
120+ t .Parallel ()
121+ a := assert .New (t )
122+
123+ // Test v2 endpoint response format (uses 'id' field)
124+ v2Response := `{"id":"123456789","email":"[email protected] ","name":"Test User"}` 125+ var userV2 struct {
126+ ID string `json:"id"`
127+ Sub string `json:"sub"`
128+ Email string `json:"email"`
129+ Name string `json:"name"`
130+ }
131+ err := json .Unmarshal ([]byte (v2Response ), & userV2 )
132+ a .NoError (err )
133+ a .Equal ("123456789" , userV2 .ID )
134+ a .Equal ("" , userV2 .Sub )
135+
136+ // Test OpenID Connect response format (uses 'sub' field)
137+ oidcResponse := `{"sub":"123456789","email":"[email protected] ","name":"Test User"}` 138+ var userOIDC struct {
139+ ID string `json:"id"`
140+ Sub string `json:"sub"`
141+ Email string `json:"email"`
142+ Name string `json:"name"`
143+ }
144+ err = json .Unmarshal ([]byte (oidcResponse ), & userOIDC )
145+ a .NoError (err )
146+ a .Equal ("" , userOIDC .ID )
147+ a .Equal ("123456789" , userOIDC .Sub )
148+ }
149+
118150func googleProvider () * google.Provider {
119151 return google .New (os .Getenv ("GOOGLE_KEY" ), os .Getenv ("GOOGEL_SECRET" ), "/foo" )
120152}
0 commit comments