11package org .gitlab4j .api ;
22
3+ import java .util .Map ;
4+
5+ import javax .ws .rs .core .Response ;
6+
37import org .gitlab4j .api .Constants .TokenType ;
8+ import org .gitlab4j .api .models .OauthTokenResponse ;
49import org .gitlab4j .api .models .Session ;
510import org .gitlab4j .api .models .User ;
611import org .gitlab4j .api .models .Version ;
712
8- import javax .ws .rs .core .Response ;
9- import java .util .Map ;
10-
1113/**
1214 * This class is provides a simplified interface to a GitLab API server, and divides the API up into
1315 * a separate API class for each concern.
@@ -19,7 +21,7 @@ public class GitLabApi {
1921
2022 /** Specifies the version of the GitLab API to communicate with. */
2123 public enum ApiVersion {
22- V3 , V4 ;
24+ V3 , V4 , OAUTH2_CLIENT ;
2325
2426 public String getApiNamespace () {
2527 return ("/api/" + name ().toLowerCase ());
@@ -51,6 +53,102 @@ public String getApiNamespace() {
5153 private Session session ;
5254
5355
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+
54152 /**
55153 * <p>Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance
56154 * using returned private token and the specified GitLab API version.</p>
@@ -65,7 +163,7 @@ public String getApiNamespace() {
65163 * @throws GitLabApiException GitLabApiException if any exception occurs during execution
66164 */
67165 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 ));
69167 }
70168
71169 /**
@@ -81,7 +179,7 @@ public static GitLabApi login(ApiVersion apiVersion, String url, String username
81179 * @throws GitLabApiException GitLabApiException if any exception occurs during execution
82180 */
83181 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 ));
85183 }
86184
87185 /**
@@ -105,12 +203,22 @@ public static GitLabApi login(ApiVersion apiVersion, String url, String username
105203 gitLabApi .setIgnoreCertificateErrors (true );
106204 }
107205
108- SessionApi sessionApi = gitLabApi .getSessionApi ();
109- Session session = sessionApi .login (username , null , password );
110- gitLabApi = new GitLabApi (apiVersion , url , session );
206+ try {
111207
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+ }
114222 }
115223
116224 return (gitLabApi );
@@ -130,7 +238,7 @@ public static GitLabApi login(ApiVersion apiVersion, String url, String username
130238 * @throws GitLabApiException GitLabApiException if any exception occurs during execution
131239 */
132240 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 ));
134242 }
135243
136244 /**
@@ -148,7 +256,7 @@ public static GitLabApi login(String url, String username, String password, bool
148256 */
149257 @ Deprecated
150258 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 ));
152260 }
153261
154262 /**
0 commit comments