39
39
import io .airbyte .validation .json .JsonValidationException ;
40
40
import java .io .IOException ;
41
41
import java .net .URI ;
42
+ import java .net .URISyntaxException ;
42
43
import java .net .URLEncoder ;
43
44
import java .net .http .HttpClient ;
44
45
import java .net .http .HttpClient .Version ;
45
46
import java .net .http .HttpRequest ;
46
47
import java .net .http .HttpResponse ;
47
48
import java .nio .charset .StandardCharsets ;
48
- import java .util .List ;
49
49
import java .util .Map ;
50
50
import java .util .Optional ;
51
51
import java .util .UUID ;
52
+ import org .apache .http .client .utils .URIBuilder ;
52
53
53
54
/**
54
55
* Following docs from https://developers.google.com/identity/protocols/oauth2/web-server
@@ -61,7 +62,7 @@ public class GoogleOAuthFlow implements OAuthFlowImplementation {
61
62
private final static String ACCESS_TOKEN_URL = "https://oauth2.googleapis.com/token" ;
62
63
63
64
private final String scope ;
64
- private final List <String > googleQueryParameters ;
65
+ private final Map <String , String > defaultQueryParams ;
65
66
66
67
private final ConfigRepository configRepository ;
67
68
@@ -73,13 +74,15 @@ public GoogleOAuthFlow(ConfigRepository configRepository, String scope) {
73
74
GoogleOAuthFlow (ConfigRepository configRepository , String scope , HttpClient httpClient ) {
74
75
this .configRepository = configRepository ;
75
76
this .httpClient = httpClient ;
76
- this .scope = UrlEncode (scope );
77
- this .googleQueryParameters = List .of (
78
- String .format ("scope=%s" , this .scope ),
79
- "access_type=offline" ,
80
- "include_granted_scopes=true" ,
81
- "response_type=code" ,
82
- "prompt=consent" );
77
+ this .scope = scope ;
78
+ this .defaultQueryParams = ImmutableMap .<String , String >builder ()
79
+ .put ("scope" , this .scope )
80
+ .put ("access_type" , "offline" )
81
+ .put ("include_granted_scopes" , "true" )
82
+ .put ("response_type" , "code" )
83
+ .put ("prompt" , "consent" )
84
+ .build ();
85
+
83
86
}
84
87
85
88
@ Override
@@ -96,18 +99,18 @@ public String getDestinationConsentUrl(UUID workspaceId, UUID destinationDefinit
96
99
}
97
100
98
101
private String getConsentUrl (UUID definitionId , String clientId , String redirectUrl ) {
99
- final StringBuilder result = new StringBuilder (CONSENT_URL )
100
- .append ("?" );
101
- for (String queryParameter : googleQueryParameters ) {
102
- result .append (queryParameter ).append ("&" );
102
+ try {
103
+ URIBuilder uriBuilder = new URIBuilder (CONSENT_URL )
104
+ .addParameter ("state" , definitionId .toString ())
105
+ .addParameter ("client_id" , clientId )
106
+ .addParameter ("redirect_uri" , redirectUrl );
107
+ for (Map .Entry <String , String > queryParameter : defaultQueryParams .entrySet ()) {
108
+ uriBuilder .addParameter (queryParameter .getKey (), queryParameter .getValue ());
109
+ }
110
+ return uriBuilder .toString ();
111
+ } catch (URISyntaxException e ) {
112
+ throw new IllegalArgumentException (e );
103
113
}
104
- return result
105
- // TODO state should be randomly generated, and the 2nd step of oauth should verify its value
106
- // matches the initially generated state value
107
- .append ("state=" ).append (definitionId .toString ()).append ("&" )
108
- .append ("client_id=" ).append (clientId ).append ("&" )
109
- .append ("redirect_uri=" ).append (redirectUrl )
110
- .toString ();
111
114
}
112
115
113
116
@ Override
0 commit comments