Skip to content

Commit

Permalink
Update OIDCConfig with scope information (#13973)
Browse files Browse the repository at this point in the history
Allow users to provide custom scope through OIDC configuration
  • Loading branch information
findingrish authored Mar 28, 2023
1 parent d5b1b5b commit e8e8082
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/development/extensions-core/druid-pac4j.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ druid.auth.authenticator.jwt.type=jwt
|`druid.auth.pac4j.oidc.clientSecret`|OAuth Client Application secret. It can be provided as plaintext string or The [Password Provider](../../operations/password-provider.md).|none|Yes|
|`druid.auth.pac4j.oidc.discoveryURI`|discovery URI for fetching OP metadata [see this](http://openid.net/specs/openid-connect-discovery-1_0.html).|none|Yes|
|`druid.auth.pac4j.oidc.oidcClaim`|[claim](https://openid.net/specs/openid-connect-core-1_0.html#Claims) that will be extracted from the ID Token after validation.|name|No|
|`druid.auth.pac4j.oidc.scope`| scope is used by an application during authentication to authorize access to a user's details |`openid profile email`|No
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.google.common.base.Preconditions;
import org.apache.druid.metadata.PasswordProvider;

import javax.annotation.Nullable;

public class OIDCConfig
{
private final String DEFAULT_SCOPE = "name";
Expand All @@ -39,18 +41,23 @@ public class OIDCConfig
@JsonProperty
private final String oidcClaim;

@JsonProperty
private final String scope;

@JsonCreator
public OIDCConfig(
@JsonProperty("clientID") String clientID,
@JsonProperty("clientSecret") PasswordProvider clientSecret,
@JsonProperty("discoveryURI") String discoveryURI,
@JsonProperty("oidcClaim") String oidcClaim
@JsonProperty("oidcClaim") String oidcClaim,
@JsonProperty("scope") @Nullable String scope
)
{
this.clientID = Preconditions.checkNotNull(clientID, "null clientID");
this.clientSecret = Preconditions.checkNotNull(clientSecret, "null clientSecret");
this.discoveryURI = Preconditions.checkNotNull(discoveryURI, "null discoveryURI");
this.oidcClaim = oidcClaim == null ? DEFAULT_SCOPE : oidcClaim;
this.scope = scope;
}

@JsonProperty
Expand All @@ -76,4 +83,10 @@ public String getOidcClaim()
{
return oidcClaim;
}

@JsonProperty
public String getScope()
{
return scope;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private Config createPac4jConfig(OIDCConfig oidcConfig)
oidcConf.setClientId(oidcConfig.getClientID());
oidcConf.setSecret(oidcConfig.getClientSecret().getPassword());
oidcConf.setDiscoveryURI(oidcConfig.getDiscoveryURI());
oidcConf.setScope(oidcConfig.getScope());
oidcConf.setExpireSessionWithToken(true);
oidcConf.setUseNonce(true);
oidcConf.setReadTimeout(Ints.checkedCast(pac4jCommonConfig.getReadTimeout().getMillis()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public void testSerde() throws Exception
String jsonStr = "{\n"
+ " \"clientID\": \"testid\",\n"
+ " \"clientSecret\": \"testsecret\",\n"
+ " \"discoveryURI\": \"testdiscoveryuri\"\n"
+ " \"discoveryURI\": \"testdiscoveryuri\",\n"
+ " \"scope\": \"testscope\"\n"
+ "}\n";

OIDCConfig conf = jsonMapper.readValue(
Expand All @@ -44,6 +45,7 @@ public void testSerde() throws Exception
Assert.assertEquals("testsecret", conf.getClientSecret().getPassword());
Assert.assertEquals("testdiscoveryuri", conf.getDiscoveryURI());
Assert.assertEquals("name", conf.getOidcClaim());
Assert.assertEquals("testscope", conf.getScope());
}

@Test
Expand All @@ -55,7 +57,8 @@ public void testSerdeWithoutDefaults() throws Exception
+ " \"clientID\": \"testid\",\n"
+ " \"clientSecret\": \"testsecret\",\n"
+ " \"discoveryURI\": \"testdiscoveryuri\",\n"
+ " \"oidcClaim\": \"email\"\n"
+ " \"oidcClaim\": \"email\",\n"
+ " \"scope\": \"testscope\"\n"
+ "}\n";

OIDCConfig conf = jsonMapper.readValue(
Expand All @@ -67,5 +70,6 @@ public void testSerdeWithoutDefaults() throws Exception
Assert.assertEquals("testsecret", conf.getClientSecret().getPassword());
Assert.assertEquals("testdiscoveryuri", conf.getDiscoveryURI());
Assert.assertEquals("email", conf.getOidcClaim());
Assert.assertEquals("testscope", conf.getScope());
}
}

0 comments on commit e8e8082

Please sign in to comment.