@@ -39,6 +39,46 @@ func TestGetToken(t *testing.T) {
3939 t .Errorf ("scope wants %s but %s" , w , r .Scope )
4040 return fmt .Sprintf ("%s?error=invalid_scope" , r .RedirectURI )
4141 }
42+ redirectURIPrefix := "http://localhost:"
43+ if ! strings .HasPrefix (r .RedirectURI , redirectURIPrefix ) {
44+ t .Errorf ("redirect_uri wants prefix %s but was %s" , redirectURIPrefix , r .RedirectURI )
45+ return fmt .Sprintf ("%s?error=invalid_redirect_uri" , r .RedirectURI )
46+ }
47+ return fmt .Sprintf ("%s?state=%s&code=%s" , r .RedirectURI , r .State , "AUTH_CODE" )
48+ },
49+ NewTokenResponse : func (r authserver.TokenRequest ) (int , string ) {
50+ if w := "AUTH_CODE" ; r .Code != w {
51+ t .Errorf ("code wants %s but %s" , w , r .Code )
52+ return 400 , invalidGrantResponse
53+ }
54+ return 200 , validTokenResponse
55+ },
56+ }
57+ successfulTest (t , cfg , h )
58+ })
59+
60+ t .Run ("RedirectURLHostname" , func (t * testing.T ) {
61+ cfg := oauth2cli.Config {
62+ OAuth2Config : oauth2.Config {
63+ ClientID : "YOUR_CLIENT_ID" ,
64+ ClientSecret : "YOUR_CLIENT_SECRET" ,
65+ Scopes : []string {"email" , "profile" },
66+ },
67+ RedirectURLHostname : "127.0.0.1" ,
68+ LocalServerMiddleware : loggingMiddleware (t ),
69+ }
70+ h := & authserver.Handler {
71+ T : t ,
72+ NewAuthorizationResponse : func (r authserver.AuthorizationRequest ) string {
73+ if w := "email profile" ; r .Scope != w {
74+ t .Errorf ("scope wants %s but %s" , w , r .Scope )
75+ return fmt .Sprintf ("%s?error=invalid_scope" , r .RedirectURI )
76+ }
77+ redirectURIPrefix := "http://127.0.0.1:"
78+ if ! strings .HasPrefix (r .RedirectURI , redirectURIPrefix ) {
79+ t .Errorf ("redirect_uri wants prefix %s but was %s" , redirectURIPrefix , r .RedirectURI )
80+ return fmt .Sprintf ("%s?error=invalid_redirect_uri" , r .RedirectURI )
81+ }
4282 return fmt .Sprintf ("%s?state=%s&code=%s" , r .RedirectURI , r .State , "AUTH_CODE" )
4383 },
4484 NewTokenResponse : func (r authserver.TokenRequest ) (int , string ) {
@@ -70,8 +110,9 @@ func TestGetToken(t *testing.T) {
70110 t .Errorf ("scope wants %s but %s" , w , r .Scope )
71111 return fmt .Sprintf ("%s?error=invalid_scope" , r .RedirectURI )
72112 }
73- if ! strings .HasPrefix (r .RedirectURI , "https://" ) {
74- t .Errorf ("redirect_uri must start with https:// when using TLS config %s" , r .RedirectURI )
113+ redirectURIPrefix := "https://localhost:"
114+ if ! strings .HasPrefix (r .RedirectURI , redirectURIPrefix ) {
115+ t .Errorf ("redirect_uri wants prefix %s but was %s" , redirectURIPrefix , r .RedirectURI )
75116 return fmt .Sprintf ("%s?error=invalid_redirect_uri" , r .RedirectURI )
76117 }
77118 return fmt .Sprintf ("%s?state=%s&code=%s" , r .RedirectURI , r .State , "AUTH_CODE" )
0 commit comments