Skip to content

Commit 193abc3

Browse files
authored
Merge pull request #26 from maximizeIT/cctech-added-email-username-to-ssodata
Added username and primary email address to SSO token data
2 parents 7145b54 + 0f4c697 commit 193abc3

2 files changed

Lines changed: 55 additions & 5 deletions

File tree

src/main/java/com/staffbase/plugins/sdk/sso/SSOData.java

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* SSO implementation, based on this doc:
3-
* https://developers.staffbase.com/api/plugin-sso/
3+
* https://developers.staffbase.com/guide/customplugin-overview
44
*
5-
* @copyright 2017 Staffbase GmbH.
5+
* @copyright 2020 Staffbase GmbH.
66
* @author Thilo Schmalfuß
77
* @author Vitaliy Ivanov
88
* @license http://www.apache.org/licenses/LICENSE-2.0
@@ -74,13 +74,23 @@ public class SSOData {
7474
*/
7575
public static final String KEY_USER_EXTERNAL_ID = "external_id";
7676

77+
/**
78+
* The key in the JWT claims for fetching the requesting user's username.
79+
*/
80+
public static final String KEY_USER_USERNAME= "username";
81+
82+
/**
83+
* The key in the JWT claims for fetching the requesting user's primary email address.
84+
*/
85+
public static final String KEY_USER_PRIMARY_EMAIL_ADDRESS = "primary_email_address";
86+
7787
/**
7888
* The key in the JWT claims for fetching the requesting user's first name.
7989
*/
8090
public static final String KEY_USER_FIRST_NAME = "given_name";
8191

8292
/**
83-
* The key in the JWT claims for fetching the requesting users last name.
93+
* The key in the JWT claims for fetching the requesting user's last name.
8494
*/
8595
public static final String KEY_USER_LAST_NAME = "family_name";
8696

@@ -215,6 +225,16 @@ public class SSOData {
215225
*/
216226
private final String userExternalID;
217227

228+
/**
229+
* The username of the requesting user, if given.
230+
*/
231+
private final String userUsername;
232+
233+
/**
234+
* The primary email address of the requesting user, if given.
235+
*/
236+
private final String userPrimaryEmailAddress;
237+
218238
/**
219239
* The first, i.e. given name of the user making the request using staffbase's SSO.
220240
*/
@@ -264,6 +284,8 @@ public SSOData(final JwtClaims jwtClaims) throws MalformedClaimException {
264284
this.sessionID = jwtClaims.getClaimValue(KEY_SESSION_ID, String.class);
265285
this.userID = jwtClaims.getClaimValue(KEY_USER_ID, String.class);
266286
this.userExternalID = jwtClaims.getClaimValue(KEY_USER_EXTERNAL_ID, String.class);
287+
this.userUsername = jwtClaims.getClaimValue(KEY_USER_USERNAME, String.class);
288+
this.userPrimaryEmailAddress = jwtClaims.getClaimValue(KEY_USER_PRIMARY_EMAIL_ADDRESS, String.class);
267289
this.userFirstName = jwtClaims.getClaimValue(KEY_USER_FIRST_NAME, String.class);
268290
this.userLastName = jwtClaims.getClaimValue(KEY_USER_LAST_NAME, String.class);
269291
this.userRole = jwtClaims.getClaimValue(KEY_USER_ROLE, String.class);
@@ -403,6 +425,26 @@ public Optional<String> getUserExternalID() {
403425
return Optional.ofNullable(this.userExternalID);
404426
}
405427

428+
/**
429+
* Get the username of the requesting user, if given.
430+
*
431+
* @see #userUsername
432+
* @return the requesting user's username
433+
*/
434+
public Optional<String> getUserUsername() {
435+
return Optional.ofNullable(this.userUsername);
436+
}
437+
438+
/**
439+
* Get the primary email address of the requesting user, if given.
440+
*
441+
* @see #userPrimaryEmailAddress
442+
* @return the requesting user's primary email address
443+
*/
444+
public Optional<String> getUserPrimaryEmailAddress() {
445+
return Optional.ofNullable(this.userPrimaryEmailAddress);
446+
}
447+
406448
/**
407449
* Get the first, i.e. given name of the user making the request using staffbase's
408450
* SSO.
@@ -502,6 +544,8 @@ public String toString() {
502544
", branchSlug="+ this.branchSlug+
503545
", userID="+ this.userID+
504546
", userExternalID="+ this.userExternalID+
547+
", userUsername="+ this.userUsername+
548+
", userPrimaryEmailAddress="+ this.userPrimaryEmailAddress+
505549
", userFirstName="+ this.userFirstName+
506550
", userLastName="+ this.userLastName+
507551
", userRole="+ this.userRole+

src/test/java/com/staffbase/plugins/sdk/sso/SSODataTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* SSO implementation test, based on this doc:
3-
* https://developers.staffbase.com/api/plugin-sso/
3+
* https://developers.staffbase.com/guide/customplugin-overview
44
*
5-
* @copyright 2017 Staffbase GmbH.
5+
* @copyright 2020 Staffbase GmbH.
66
* @author Thilo Schmalfuß
77
* @author Vitaliy Ivanov
88
* @license http://www.apache.org/licenses/LICENSE-2.0
@@ -37,6 +37,8 @@ public class SSODataTest {
3737
public static final String DATA_BRANCH_SLUG = "staffbasetest";
3838
public static final String DATA_USER_ID = "541954c3e4b08bbdce1a340a";
3939
public static final String DATA_USER_EXTERNAL_ID = "jdoe";
40+
public static final String DATA_USER_USERNAME = "john.doe";
41+
public static final String DATA_USER_PRIMARY_EMAIL_ADDRESS = "jdoe@email.com";
4042
public static final String DATA_USER_FIRST_NAME = "John";
4143
public static final String DATA_USER_LAST_NAME = "Doe";
4244
public static final String DATA_USER_ROLE = "editor";
@@ -74,6 +76,8 @@ public void createWithJwtClaims() throws MalformedClaimException {
7476
when(claims.getClaimValue(SSOData.KEY_SESSION_ID, String.class)).thenReturn(DATA_SESSION_ID);
7577
when(claims.getClaimValue(SSOData.KEY_USER_ID, String.class)).thenReturn(DATA_USER_ID);
7678
when(claims.getClaimValue(SSOData.KEY_USER_EXTERNAL_ID, String.class)).thenReturn(DATA_USER_EXTERNAL_ID);
79+
when(claims.getClaimValue(SSOData.KEY_USER_USERNAME, String.class)).thenReturn(DATA_USER_USERNAME);
80+
when(claims.getClaimValue(SSOData.KEY_USER_PRIMARY_EMAIL_ADDRESS, String.class)).thenReturn(DATA_USER_PRIMARY_EMAIL_ADDRESS);
7781
when(claims.getClaimValue(SSOData.KEY_USER_FIRST_NAME, String.class)).thenReturn(DATA_USER_FIRST_NAME);
7882
when(claims.getClaimValue(SSOData.KEY_USER_LAST_NAME, String.class)).thenReturn(DATA_USER_LAST_NAME);
7983
when(claims.getClaimValue(SSOData.KEY_USER_ROLE, String.class)).thenReturn(DATA_USER_ROLE);
@@ -95,6 +99,8 @@ public void createWithJwtClaims() throws MalformedClaimException {
9599
assertEquals(DATA_SESSION_ID, ssoData.getSessionId().get());
96100
assertEquals(DATA_USER_ID, ssoData.getUserID().get());
97101
assertEquals(DATA_USER_EXTERNAL_ID, ssoData.getUserExternalID().get());
102+
assertEquals(DATA_USER_USERNAME, ssoData.getUserUsername().get());
103+
assertEquals(DATA_USER_PRIMARY_EMAIL_ADDRESS, ssoData.getUserPrimaryEmailAddress().get());
98104
assertEquals(DATA_USER_FIRST_NAME, ssoData.getUserFirstName().get());
99105
assertEquals(DATA_USER_LAST_NAME, ssoData.getUserLastName().get());
100106
assertEquals(DATA_USER_ROLE, ssoData.getUserRole().get());

0 commit comments

Comments
 (0)