Skip to content

Commit fe14e34

Browse files
authoredMar 7, 2025
ApplicationsApi improvements (#1235)
* Add "confidential" parameter in "createApplication(..)" * Fix bug when multiple scopes are provided * Add "renewSecret(..)" method * Improve "Application" model - add "confidential" and "secret" attributes - add missing "toString()" method
1 parent 6e91cb3 commit fe14e34

File tree

4 files changed

+106
-12
lines changed

4 files changed

+106
-12
lines changed
 

‎gitlab4j-api/src/main/java/org/gitlab4j/api/ApplicationsApi.java

+40-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.stream.Collectors;
66
import java.util.stream.Stream;
77

8+
import jakarta.ws.rs.core.Form;
89
import jakarta.ws.rs.core.GenericType;
910
import jakarta.ws.rs.core.Response;
1011

@@ -83,7 +84,9 @@ public Stream<Application> getApplicationsStream() throws GitLabApiException {
8384
* @param scopes the scopes of the application (api, read_user, sudo, read_repository, openid, profile, email)
8485
* @return the created Application instance
8586
* @throws GitLabApiException if any exception occurs
87+
* @deprecated use {@link #createApplication(String, String, List, Boolean)} instead
8688
*/
89+
@Deprecated
8790
public Application createApplication(String name, String redirectUri, ApplicationScope[] scopes)
8891
throws GitLabApiException {
8992

@@ -104,19 +107,40 @@ public Application createApplication(String name, String redirectUri, Applicatio
104107
* @param scopes the scopes of the application (api, read_user, sudo, read_repository, openid, profile, email)
105108
* @return the created Application instance
106109
* @throws GitLabApiException if any exception occurs
110+
* @deprecated use {@link #createApplication(String, String, List, Boolean)} instead
107111
*/
112+
@Deprecated
108113
public Application createApplication(String name, String redirectUri, List<ApplicationScope> scopes)
109114
throws GitLabApiException {
115+
return createApplication(name, redirectUri, scopes, null);
116+
}
117+
118+
/**
119+
* Create an OAUTH Application.
120+
*
121+
* <pre><code>GitLab Endpoint: POST /api/v4/applications</code></pre>
122+
*
123+
* @param name the name for the OAUTH Application
124+
* @param redirectUri the redirect URI for the OAUTH Application
125+
* @param scopes the scopes of the application (api, read_user, sudo, read_repository, openid, profile, email)
126+
* @param confidential The application is used where the client secret can be kept confidential. Native mobile apps and Single Page Apps are considered non-confidential
127+
* @return the created Application instance
128+
* @throws GitLabApiException if any exception occurs
129+
*/
130+
public Application createApplication(
131+
String name, String redirectUri, List<ApplicationScope> scopes, Boolean confidential)
132+
throws GitLabApiException {
110133

111134
if (scopes == null || scopes.isEmpty()) {
112135
throw new GitLabApiException("scopes cannot be null or empty");
113136
}
114137

115-
String scopesString = scopes.stream().map(ApplicationScope::toString).collect(Collectors.joining(","));
138+
String scopesString = scopes.stream().map(ApplicationScope::toString).collect(Collectors.joining(" "));
116139
GitLabApiForm formData = new GitLabApiForm()
117140
.withParam("name", name, true)
118141
.withParam("redirect_uri", redirectUri, true)
119-
.withParam("scopes", scopesString, true);
142+
.withParam("scopes", scopesString, true)
143+
.withParam("confidential", confidential);
120144
Response response = post(Response.Status.CREATED, formData, "applications");
121145
return (response.readEntity(Application.class));
122146
}
@@ -132,4 +156,18 @@ public Application createApplication(String name, String redirectUri, List<Appli
132156
public void deleteApplication(Long applicationId) throws GitLabApiException {
133157
delete(Response.Status.NO_CONTENT, null, "applications", applicationId);
134158
}
159+
160+
/**
161+
* Renews an application secret.
162+
*
163+
* <pre><code>GitLab Endpoint: POST /applications/:id/renew-secret</code></pre>
164+
*
165+
* @param applicationId the ID of the OUAUTH Application to renew
166+
* @return the updated Application instance
167+
* @throws GitLabApiException if any exception occurs
168+
*/
169+
public Application renewSecret(Long applicationId) throws GitLabApiException {
170+
Response response = post(Response.Status.CREATED, (Form) null, "applications", applicationId, "renew-secret");
171+
return (response.readEntity(Application.class));
172+
}
135173
}

‎gitlab4j-models/src/main/java/org/gitlab4j/api/models/Application.java

+25
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import java.io.Serializable;
44

5+
import org.gitlab4j.models.utils.JacksonJson;
6+
57
public class Application implements Serializable {
68
private static final long serialVersionUID = 1L;
79

810
private Long id;
911
private String applicationId;
1012
private String applicationName;
1113
private String callbackUrl;
14+
private Boolean confidential;
15+
private String secret;
1216

1317
public Long getId() {
1418
return id;
@@ -41,4 +45,25 @@ public String getCallbackUrl() {
4145
public void setCallbackUrl(String callbackUrl) {
4246
this.callbackUrl = callbackUrl;
4347
}
48+
49+
public Boolean getConfidential() {
50+
return confidential;
51+
}
52+
53+
public void setConfidential(Boolean confidential) {
54+
this.confidential = confidential;
55+
}
56+
57+
public String getSecret() {
58+
return secret;
59+
}
60+
61+
public void setSecret(String secret) {
62+
this.secret = secret;
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return (JacksonJson.toJsonString(this));
68+
}
4469
}

‎gitlab4j-models/src/main/java/org/gitlab4j/models/Constants.java

+38-9
Original file line numberDiff line numberDiff line change
@@ -829,26 +829,55 @@ public String toString() {
829829
* Enum for the various Application scope values.
830830
*/
831831
public enum ApplicationScope {
832-
833-
/** Access the authenticated user's API */
832+
/** Grants complete read/write access to the API, including all groups and projects, the container registry, the dependency proxy, and the package registry. */
834833
API,
835834

836-
/** Read the authenticated user's personal information */
835+
/** Grants read access to the API, including all groups and projects, the container registry, and the package registry. */
836+
READ_API,
837+
838+
/** Grants read-only access to your profile through the /user API endpoint, which includes username, public email, and full name. Also grants access to read-only API endpoints under /users. */
837839
READ_USER,
838840

839-
/** Perform API actions as any user in the system */
840-
SUDO,
841+
/** Grants create access to the runners. */
842+
CREATE_RUNNER,
841843

842-
/** Allows read-access to the repository */
844+
/** Grants access to manage the runners. */
845+
MANAGE_RUNNER,
846+
847+
/** Grants permission to perform Kubernetes API calls using the agent for Kubernetes. */
848+
K8S_PROXY,
849+
850+
/** Grants read-only access to repositories on private projects using Git-over-HTTP or the Repository Files API. */
843851
READ_REPOSITORY,
844852

845-
/** Authenticate using OpenID Connect */
853+
/** Grants read-write access to repositories on private projects using Git-over-HTTP (not using the API). */
854+
WRITE_REPOSITORY,
855+
856+
/** Grants read-only access to GitLab Observability. */
857+
READ_OBSERVABILITY,
858+
859+
/** Grants write access to GitLab Observability. */
860+
WRITE_OBSERVABILITY,
861+
862+
/** Grants access to GitLab Duo related API endpoints. */
863+
AI_FEATURES,
864+
865+
/** Grants permission to perform API actions as any user in the system, when authenticated as an admin user. */
866+
SUDO,
867+
868+
/** Grants permission to perform API actions as an administrator, when Admin Mode is enabled. */
869+
ADMIN_MODE,
870+
871+
/** Grant access to download Service Ping payload via API when authenticated as an admin user. */
872+
READ_SERVICE_PING,
873+
874+
/** Grants permission to authenticate with GitLab using OpenID Connect. Also gives read-only access to the user's profile and group memberships. */
846875
OPENID,
847876

848-
/** Allows read-only access to the user's personal information using OpenID Connect */
877+
/** Grants read-only access to the user's profile data using OpenID Connect. */
849878
PROFILE,
850879

851-
/** Allows read-only access to the user's primary email address using OpenID Connect */
880+
/** Grants read-only access to the user's primary email address using OpenID Connect. */
852881
EMAIL;
853882

854883
private static JacksonJsonEnumHelper<ApplicationScope> enumHelper =

‎gitlab4j-models/src/test/resources/org/gitlab4j/models/applications.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"id": 1,
44
"application_id": "5832fc6e14300a0d962240a8144466eef4ee93ef0d218477e55f11cf12fc3737",
55
"application_name": "MyApplication",
6-
"callback_url": "http://redirect.uri"
6+
"secret": "ee1dd64b6adc89cf7e2c23099301ccc2c61b441064e9324d963c46902a85ec34",
7+
"callback_url": "http://redirect.uri",
8+
"confidential": true
79
}
810
]

0 commit comments

Comments
 (0)