Skip to content

Commit 24f94af

Browse files
shikher-srivastavaShikherzbynekkrisstern
authored
Read API scope support (#219)
* Add configurable OAuth scope (Fix #118) * Improve OAuth scope UX with dropdown and tooltip * cleanup * Revise OAuth scope help text for clarity Updated the description of the OAuth scope and adjusted recommendations. * README.md: docs updated for read_api scope * Update src/main/webapp/help/realm/scope-help.html Co-authored-by: Zbynek Konecny <zbynek1729@gmail.com> --------- Co-authored-by: Shikher <shikher@example.com> Co-authored-by: Zbynek Konecny <zbynek1729@gmail.com> Co-authored-by: Kris Stern <88480540+krisstern@users.noreply.github.com>
1 parent 5640e63 commit 24f94af

4 files changed

Lines changed: 45 additions & 5 deletions

File tree

docs/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ also supported.
77
## Setup
88

99
Before configuring the plugin you must create a GitLab application
10-
registration. In the Scopes section mark **api**.
10+
registration. In the Scopes section mark **read_api** (recommended) or
11+
**api**.
1112

12-
the authorization callback URL takes a specific value. It must be
13+
The authorization callback URL takes a specific value. It must be
1314
`http://myserver.example.com:8080/securityRealm/finishLogin` where
1415
myserver.example.com:8080 is the location of the Jenkins server.
1516

@@ -28,8 +29,9 @@ realm to authenticate Jenkins users via GitLab OAuth.
2829
**GitLab Authentication Plugin**.
2930
2. The settings to configure are: GitLab Web URI, GitLab API URI,
3031
Client ID, Client Secret, and OAuth Scope(s).
31-
3. If you're using GitLab Enterprise then the API URI is
32-
<https://ghe.acme.com/api/v3>. The prefix
32+
3. Select the scope configured during GitLab application registration in the **OAuth Scope(s)** dropdown.
33+
4. If you're using GitLab Enterprise then the API URI is
34+
<https://ghe.acme.com/api/v3>. The prefix
3335
"[api/v3](https://ghe.acme.com/api/v3)" will be
3436
completed by the plugin
3537

src/main/java/org/jenkinsci/plugins/GitLabSecurityRealm.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import hudson.security.SecurityRealm;
4444
import hudson.security.UserMayOrMayNotExistException2;
4545
import hudson.tasks.Mailer;
46+
import hudson.util.ListBoxModel;
4647
import hudson.util.Secret;
4748
import jakarta.servlet.http.HttpSession;
4849
import java.io.IOException;
@@ -77,6 +78,7 @@
7778
import org.gitlab4j.models.Constants.TokenType;
7879
import org.jfree.util.Log;
7980
import org.kohsuke.stapler.DataBoundConstructor;
81+
import org.kohsuke.stapler.DataBoundSetter;
8082
import org.kohsuke.stapler.Header;
8183
import org.kohsuke.stapler.HttpRedirect;
8284
import org.kohsuke.stapler.HttpResponse;
@@ -107,6 +109,7 @@ public class GitLabSecurityRealm extends SecurityRealm {
107109
private String gitlabApiUri;
108110
private String clientID;
109111
private Secret clientSecret;
112+
private String scope = "api";
110113

111114
/**
112115
* @param gitlabWebUri
@@ -199,6 +202,10 @@ public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingC
199202
writer.startNode("clientSecret");
200203
writer.setValue(realm.clientSecret.getEncryptedValue());
201204
writer.endNode();
205+
206+
writer.startNode("scope");
207+
writer.setValue(realm.getScope());
208+
writer.endNode();
202209
}
203210

204211
@Override
@@ -234,6 +241,9 @@ private void setValue(GitLabSecurityRealm realm, String node, String value) {
234241
case "gitlabapiuri":
235242
realm.setGitlabApiUri(value);
236243
break;
244+
case "scope":
245+
realm.setScope(value);
246+
break;
237247
default:
238248
throw new ConversionException("Invalid node value = " + node);
239249
}
@@ -263,6 +273,15 @@ public Secret getClientSecret() {
263273
return clientSecret;
264274
}
265275

276+
public String getScope() {
277+
return scope;
278+
}
279+
280+
@DataBoundSetter
281+
public void setScope(String scope) {
282+
this.scope = scope;
283+
}
284+
266285
// "from" is coming from SecurityRealm/loginLink.jelly
267286
public HttpResponse doCommenceLogin(
268287
StaplerRequest2 request, @QueryParameter String from, @Header("Referer") final String referer)
@@ -290,7 +309,7 @@ public HttpResponse doCommenceLogin(
290309
parameters.add(new BasicNameValuePair("redirect_uri", buildRedirectUrl(request)));
291310
parameters.add(new BasicNameValuePair("response_type", "code"));
292311
parameters.add(new BasicNameValuePair("client_id", clientID));
293-
parameters.add(new BasicNameValuePair("scope", "api"));
312+
parameters.add(new BasicNameValuePair("scope", getScope()));
294313
parameters.add(new BasicNameValuePair("state", state));
295314

296315
return new HttpRedirect(
@@ -530,6 +549,13 @@ public DescriptorImpl() {
530549
public DescriptorImpl(Class<? extends SecurityRealm> clazz) {
531550
super(clazz);
532551
}
552+
553+
public ListBoxModel doFillScopeItems() {
554+
ListBoxModel items = new ListBoxModel();
555+
items.add("api", "api");
556+
items.add("read_api", "read_api");
557+
return items;
558+
}
533559
}
534560

535561
// Overridden for better type safety.
@@ -586,6 +612,7 @@ public boolean equals(Object object) {
586612
return this.getGitlabWebUri().equals(obj.getGitlabWebUri())
587613
&& this.getGitlabApiUri().equals(obj.getGitlabApiUri())
588614
&& this.getClientID().equals(obj.getClientID())
615+
&& this.getScope().equals(obj.getScope())
589616
&& this.clientSecret.equals(obj.clientSecret);
590617
} else {
591618
return false;

src/main/resources/org/jenkinsci/plugins/GitLabSecurityRealm/config.jelly

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@
2020
<f:password />
2121
</f:entry>
2222

23+
<f:entry title="OAuth Scope(s)" field="scope" help="/plugin/gitlab-oauth/help/realm/scope-help.html">
24+
<f:select default="api" />
25+
</f:entry>
26+
2327
</f:section>
2428
</j:jelly>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div>
2+
The <b>Token Scope</b> from the GitLab application entry being configured. Recommended value: <strong>read_api</strong>.
3+
<ul>
4+
<li><strong>api:</strong> Full read/write access to the GitLab API. (Legacy support)</li>
5+
<li><strong>read_api:</strong> Read-only access to the GitLab API. (Recommended)</li>
6+
</ul>
7+
</div>

0 commit comments

Comments
 (0)