Skip to content

Commit ba7c3e1

Browse files
committed
Add Case Sensitive check to client
1 parent fc8ac45 commit ba7c3e1

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

clientLibrary/src/main/scala/za/co/absa/loginclient/tokenRetrieval/client/TokenRetrievalClient.scala

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ case class TokenRetrievalClient(host: String) {
4545
* @param username The username used for authentication.
4646
* @param password The password associated with the provided username.
4747
* @param groups An optional list of PAM groups. If provided, only JWTs associated with these groups are returned if the user belongs to them.
48+
* @param caseSensitiveGroups A boolean indicating whether the group prefixes should be treated as case sensitive.
4849
* @return An AccessToken object representing the retrieved access token (JWT) from the login service.
4950
*/
50-
def fetchAccessToken(username: String, password: String, groups: List[String]): AccessToken = {
51-
fetchAccessAndRefreshToken(username, password, groups)._1
51+
def fetchAccessToken(
52+
username: String,
53+
password: String,
54+
groups: List[String] = List.empty,
55+
caseSensitiveGroups: Boolean = false): AccessToken = {
56+
fetchAccessAndRefreshToken(username, password, groups, caseSensitiveGroups)._1
5257
}
5358

5459
/**
@@ -58,10 +63,15 @@ case class TokenRetrievalClient(host: String) {
5863
* @param keytabLocation Optional location of the keytab file.
5964
* @param userPrincipal Optional userPrincipal name included in the above keytab file.
6065
* @param groups An optional list of PAM groups. If provided, only JWTs associated with these groups are returned if the user belongs to them.
66+
* @param caseSensitiveGroups A boolean indicating whether the group prefixes should be treated as case sensitive.
6167
* @return An AccessToken object representing the retrieved access token (JWT) from the login service.
6268
*/
63-
def fetchAccessToken(keytabLocation: Option[String], userPrincipal: Option[String], groups: List[String]): AccessToken = {
64-
fetchAccessAndRefreshToken(keytabLocation, userPrincipal, groups)._1
69+
def fetchAccessToken(
70+
keytabLocation: Option[String],
71+
userPrincipal: Option[String],
72+
groups: List[String] = List.empty,
73+
caseSensitiveGroups: Boolean = false): AccessToken = {
74+
fetchAccessAndRefreshToken(keytabLocation, userPrincipal, groups, caseSensitiveGroups)._1
6575
}
6676

6777
/**
@@ -73,7 +83,7 @@ case class TokenRetrievalClient(host: String) {
7383
* @return A RefreshToken object representing the retrieved refresh token from the login service.
7484
*/
7585
def fetchRefreshToken(keytabLocation: Option[String], userPrincipal: Option[String]): RefreshToken = {
76-
fetchAccessAndRefreshToken(keytabLocation, userPrincipal, List.empty)._2
86+
fetchAccessAndRefreshToken(keytabLocation, userPrincipal)._2
7787
}
7888

7989
/**
@@ -85,7 +95,7 @@ case class TokenRetrievalClient(host: String) {
8595
* @return A RefreshToken object representing the retrieved refresh token from the login service.
8696
*/
8797
def fetchRefreshToken(username: String, password: String): RefreshToken = {
88-
fetchAccessAndRefreshToken(username, password, List.empty)._2
98+
fetchAccessAndRefreshToken(username, password)._2
8999
}
90100

91101
/**
@@ -96,14 +106,23 @@ case class TokenRetrievalClient(host: String) {
96106
* @param username The username used for authentication.
97107
* @param password The password associated with the provided username.
98108
* @param groups An optional list of PAM groups. If provided, only JWTs associated with these groups are returned if the user belongs to them.
109+
* @param caseSensitiveGroups A boolean indicating whether the group prefixes should be treated as case sensitive.
99110
* @return A tuple containing the AccessToken and RefreshToken objects representing the retrieved access and refresh tokens (JWTs) from the login service.
100111
*/
101-
def fetchAccessAndRefreshToken(username: String, password: String, groups: List[String]): (AccessToken, RefreshToken) = {
102-
112+
def fetchAccessAndRefreshToken(
113+
username: String,
114+
password: String,
115+
groups: List[String] = List.empty,
116+
caseSensitiveGroups: Boolean = false
117+
): (AccessToken, RefreshToken) = {
103118
val issuerUri = if(groups.nonEmpty) {
104119
val commaSeparatedString = groups.mkString(",")
105120
val urlEncodedGroups = URLEncoder.encode(commaSeparatedString, "UTF-8")
106-
s"$host/token/generate?group-prefixes=$urlEncodedGroups"
121+
var uri = s"$host/token/generate?group-prefixes=$urlEncodedGroups"
122+
if(caseSensitiveGroups) {
123+
uri += "&case-sensitive=true"
124+
}
125+
uri
107126
} else s"$host/token/generate"
108127

109128
val jsonString = fetchToken(issuerUri, username, password)
@@ -121,14 +140,23 @@ case class TokenRetrievalClient(host: String) {
121140
* @param keytabLocation Optional location of the keytab file.
122141
* @param userPrincipal Optional userPrincipal name included in the above keytab file.
123142
* @param groups An optional list of PAM groups. If provided, only JWTs associated with these groups are returned if the user belongs to them.
143+
* @param caseSensitiveGroups A boolean indicating whether the group prefixes should be treated as case sensitive.
124144
* @return A tuple containing the AccessToken and RefreshToken objects representing the retrieved access and refresh tokens (JWTs) from the login service.
125145
*/
126-
def fetchAccessAndRefreshToken(keytabLocation: Option[String], userPrincipal: Option[String], groups: List[String]): (AccessToken, RefreshToken) = {
127-
146+
def fetchAccessAndRefreshToken(
147+
keytabLocation: Option[String],
148+
userPrincipal: Option[String],
149+
groups: List[String] = List.empty,
150+
caseSensitiveGroups: Boolean = false
151+
): (AccessToken, RefreshToken) = {
128152
val issuerUri = if(groups.nonEmpty) {
129153
val commaSeparatedString = groups.mkString(",")
130154
val urlEncodedGroups = URLEncoder.encode(commaSeparatedString, "UTF-8")
131-
s"$host/token/generate?group-prefixes=$urlEncodedGroups"
155+
var uri = s"$host/token/generate?group-prefixes=$urlEncodedGroups"
156+
if(caseSensitiveGroups) {
157+
uri += "&case-sensitive=true"
158+
}
159+
uri
132160
} else s"$host/token/generate"
133161

134162
val jsonString = fetchToken(issuerUri, keytabLocation, userPrincipal)

clientLibrary/src/test/scala/za/co/absa/loginclient/tokenRetrieval/client/TokenRetrievalClientTest.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class TokenRetrievalClientTest extends AnyFlatSpec with Matchers{
2525
private val dummyUri = "https://example.com"
2626
private val dummyUser = "exampleUser"
2727
private val dummyPassword = "examplePassword"
28-
private val dummyGroups = List()
2928

3029
class testTokenRetrievalClient extends TokenRetrievalClient(dummyUri) {
3130
override private[client] def fetchToken(issuerUri: String, username: String, password: String) =
@@ -39,7 +38,7 @@ class TokenRetrievalClientTest extends AnyFlatSpec with Matchers{
3938

4039
val testClient = new testTokenRetrievalClient
4140

42-
val (accessResult, refreshResult) = testClient.fetchAccessAndRefreshToken(dummyUser, dummyPassword, dummyGroups)
41+
val (accessResult, refreshResult) = testClient.fetchAccessAndRefreshToken(dummyUser, dummyPassword)
4342
accessResult shouldBe AccessToken("mock-access-token")
4443
refreshResult shouldBe RefreshToken("mock-refresh-token")
4544
}

0 commit comments

Comments
 (0)