1
1
package org .gitlab4j .api ;
2
2
3
+ import java .util .Map ;
4
+
5
+ import javax .ws .rs .core .Response ;
6
+
3
7
import org .gitlab4j .api .Constants .TokenType ;
8
+ import org .gitlab4j .api .models .OauthTokenResponse ;
4
9
import org .gitlab4j .api .models .Session ;
5
10
import org .gitlab4j .api .models .User ;
6
11
import org .gitlab4j .api .models .Version ;
7
12
8
- import javax .ws .rs .core .Response ;
9
- import java .util .Map ;
10
-
11
13
/**
12
14
* This class is provides a simplified interface to a GitLab API server, and divides the API up into
13
15
* a separate API class for each concern.
@@ -19,7 +21,7 @@ public class GitLabApi {
19
21
20
22
/** Specifies the version of the GitLab API to communicate with. */
21
23
public enum ApiVersion {
22
- V3 , V4 ;
24
+ V3 , V4 , OAUTH2_CLIENT ;
23
25
24
26
public String getApiNamespace () {
25
27
return ("/api/" + name ().toLowerCase ());
@@ -51,6 +53,102 @@ public String getApiNamespace() {
51
53
private Session session ;
52
54
53
55
56
+ /**
57
+ * <p>Logs into GitLab using OAuth2 with the provided {@code username} and {@code password},
58
+ * and creates a new {@code GitLabApi} instance using returned access token.</p>
59
+ *
60
+ * @param url GitLab URL
61
+ * @param username user name for which private token should be obtained
62
+ * @param password password for a given {@code username}
63
+ * @return new {@code GitLabApi} instance configured for a user-specific token
64
+ * @throws GitLabApiException GitLabApiException if any exception occurs during execution
65
+ */
66
+ public static GitLabApi oauth2Login (String url , String username , String password ) throws GitLabApiException {
67
+ return (GitLabApi .oauth2Login (ApiVersion .V4 , url , username , password , null , null , false ));
68
+ }
69
+
70
+ /**
71
+ * <p>Logs into GitLab using OAuth2 with the provided {@code username} and {@code password},
72
+ * and creates a new {@code GitLabApi} instance using returned access token.</p>
73
+ *
74
+ * @param url GitLab URL
75
+ * @param username user name for which private token should be obtained
76
+ * @param password password for a given {@code username}
77
+ * @param ignoreCertificateErrors if true will set up the Jersey system ignore SSL certificate errors
78
+ * @return new {@code GitLabApi} instance configured for a user-specific token
79
+ * @throws GitLabApiException GitLabApiException if any exception occurs during execution
80
+ */
81
+ public static GitLabApi oauth2Login (String url , String username , String password , boolean ignoreCertificateErrors ) throws GitLabApiException {
82
+ return (GitLabApi .oauth2Login (ApiVersion .V4 , url , username , password , null , null , ignoreCertificateErrors ));
83
+ }
84
+
85
+ /**
86
+ * <p>Logs into GitLab using OAuth2 with the provided {@code username} and {@code password},
87
+ * and creates a new {@code GitLabApi} instance using returned access token.</p>
88
+ *
89
+ * @param url GitLab URL
90
+ * @param username user name for which private token should be obtained
91
+ * @param password password for a given {@code username}
92
+ * @param secretToken use this token to validate received payloads
93
+ * @param clientConfigProperties Map instance with additional properties for the Jersey client connection
94
+ * @param ignoreCertificateErrors if true will set up the Jersey system ignore SSL certificate errors
95
+ * @return new {@code GitLabApi} instance configured for a user-specific token
96
+ * @throws GitLabApiException GitLabApiException if any exception occurs during execution
97
+ */
98
+ public static GitLabApi oauth2Login (String url , String username , String password ,
99
+ String secretToken , Map <String , Object > clientConfigProperties , boolean ignoreCertificateErrors )
100
+ throws GitLabApiException {
101
+ return (GitLabApi .oauth2Login (ApiVersion .V4 , url , username , password , secretToken , clientConfigProperties , ignoreCertificateErrors ));
102
+ }
103
+
104
+ /**
105
+ * <p>Logs into GitLab using OAuth2 with the provided {@code username} and {@code password},
106
+ * and creates a new {@code GitLabApi} instance using returned access token.</p>
107
+ *
108
+ * @param url GitLab URL
109
+ * @param apiVersion the ApiVersion specifying which version of the API to use
110
+ * @param username user name for which private token should be obtained
111
+ * @param password password for a given {@code username}
112
+ * @param secretToken use this token to validate received payloads
113
+ * @param clientConfigProperties Map instance with additional properties for the Jersey client connection
114
+ * @param ignoreCertificateErrors if true will set up the Jersey system ignore SSL certificate errors
115
+ * @return new {@code GitLabApi} instance configured for a user-specific token
116
+ * @throws GitLabApiException GitLabApiException if any exception occurs during execution
117
+ */
118
+ public static GitLabApi oauth2Login (ApiVersion apiVersion , String url , String username , String password ,
119
+ String secretToken , Map <String , Object > clientConfigProperties , boolean ignoreCertificateErrors )
120
+ throws GitLabApiException {
121
+
122
+ if (username == null || username .trim ().length () == 0 ) {
123
+ throw new IllegalArgumentException ("both username and email cannot be empty or null" );
124
+ }
125
+
126
+ GitLabApi gitLabApi = new GitLabApi (ApiVersion .OAUTH2_CLIENT , url , (String )null );
127
+ if (ignoreCertificateErrors ) {
128
+ gitLabApi .setIgnoreCertificateErrors (true );
129
+ }
130
+
131
+ class Oauth2Api extends AbstractApi {
132
+ Oauth2Api (GitLabApi gitlabApi ) {
133
+ super (gitlabApi );
134
+ }
135
+ }
136
+
137
+ GitLabApiForm formData = new GitLabApiForm ()
138
+ .withParam ("grant_type" , "password" , true )
139
+ .withParam ("username" , username , true )
140
+ .withParam ("password" , password , true );
141
+
142
+ Response response = new Oauth2Api (gitLabApi ).post (Response .Status .OK , formData , "oauth" , "token" );
143
+ OauthTokenResponse oauthToken = response .readEntity (OauthTokenResponse .class );
144
+ gitLabApi = new GitLabApi (apiVersion , url , TokenType .ACCESS , oauthToken .getAccessToken (), secretToken , clientConfigProperties );
145
+ if (ignoreCertificateErrors ) {
146
+ gitLabApi .setIgnoreCertificateErrors (true );
147
+ }
148
+
149
+ return (gitLabApi );
150
+ }
151
+
54
152
/**
55
153
* <p>Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance
56
154
* using returned private token and the specified GitLab API version.</p>
@@ -65,7 +163,7 @@ public String getApiNamespace() {
65
163
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
66
164
*/
67
165
public static GitLabApi login (ApiVersion apiVersion , String url , String username , String password ) throws GitLabApiException {
68
- return (login (apiVersion , url , username , password , false ));
166
+ return (GitLabApi . login (apiVersion , url , username , password , false ));
69
167
}
70
168
71
169
/**
@@ -81,7 +179,7 @@ public static GitLabApi login(ApiVersion apiVersion, String url, String username
81
179
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
82
180
*/
83
181
public static GitLabApi login (String url , String username , String password ) throws GitLabApiException {
84
- return (login (ApiVersion .V4 , url , username , password , false ));
182
+ return (GitLabApi . login (ApiVersion .V4 , url , username , password , false ));
85
183
}
86
184
87
185
/**
@@ -105,12 +203,22 @@ public static GitLabApi login(ApiVersion apiVersion, String url, String username
105
203
gitLabApi .setIgnoreCertificateErrors (true );
106
204
}
107
205
108
- SessionApi sessionApi = gitLabApi .getSessionApi ();
109
- Session session = sessionApi .login (username , null , password );
110
- gitLabApi = new GitLabApi (apiVersion , url , session );
206
+ try {
111
207
112
- if (ignoreCertificateErrors ) {
113
- gitLabApi .setIgnoreCertificateErrors (true );
208
+ SessionApi sessionApi = gitLabApi .getSessionApi ();
209
+ Session session = sessionApi .login (username , null , password );
210
+ gitLabApi = new GitLabApi (apiVersion , url , session );
211
+
212
+ if (ignoreCertificateErrors ) {
213
+ gitLabApi .setIgnoreCertificateErrors (true );
214
+ }
215
+
216
+ } catch (GitLabApiException gle ) {
217
+ if (gle .getHttpStatus () != Response .Status .NOT_FOUND .getStatusCode ()) {
218
+ throw (gle );
219
+ } else {
220
+ gitLabApi = GitLabApi .oauth2Login (apiVersion , url , username , password , null , null , ignoreCertificateErrors );
221
+ }
114
222
}
115
223
116
224
return (gitLabApi );
@@ -130,7 +238,7 @@ public static GitLabApi login(ApiVersion apiVersion, String url, String username
130
238
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
131
239
*/
132
240
public static GitLabApi login (String url , String username , String password , boolean ignoreCertificateErrors ) throws GitLabApiException {
133
- return (login (ApiVersion .V4 , url , username , password , ignoreCertificateErrors ));
241
+ return (GitLabApi . login (ApiVersion .V4 , url , username , password , ignoreCertificateErrors ));
134
242
}
135
243
136
244
/**
@@ -148,7 +256,7 @@ public static GitLabApi login(String url, String username, String password, bool
148
256
*/
149
257
@ Deprecated
150
258
public static GitLabApi create (String url , String username , String password ) throws GitLabApiException {
151
- return (login (url , username , password ));
259
+ return (GitLabApi . login (url , username , password ));
152
260
}
153
261
154
262
/**
0 commit comments