Skip to content

Commit b40f8a0

Browse files
authored
Merge pull request #57 from Staffbase/chore-added-branch-id-and-slug-support
chore: added support for branch ID & slug in SSO data
2 parents ab40214 + a8e2648 commit b40f8a0

7 files changed

Lines changed: 48 additions & 2 deletions

File tree

README.MD

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ The following data can be retrieved from the token:
7878

7979
|Helper Name|Token Key| Fetch Function| Description|
8080
| --- | --- | --- | --- |
81+
|CLAIM_BRANCH_ID|branch_id|getBranchId()|Get the branch ID for which the token was issued.|
82+
|CLAIM_BRANCH_SLUG|branch_slug|getBranchSlug()|Get the branch slug for which the token was issued.|
8183
|CLAIM_AUDIENCE|aud|getAudience()|Get targeted audience of the token.|
8284
|CLAIM_EXPIRE_AT|exp|getExpireAtTime()|Get the time when the token expires.|
8385
|CLAIM_NOT_BEFORE|nbf|getNotBeforeTime()|Get the time when the token starts to be valid.|
@@ -180,11 +182,10 @@ To run the tests a simple `# npm test` command in the root directory will suffic
180182

181183
## License
182184

183-
Copyright 2017-2021 Staffbase GmbH.
185+
Copyright 2017-2023 Staffbase GmbH.
184186

185187
Licensed under the Apache License, Version 2.0: https://www.apache.org/licenses/LICENSE-2.0
186188

187-
188189
<table>
189190
<tr>
190191
<td>

docs/API.MD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ SSOTokenData Class used to host the token data values and provide getter functio
3535
* [.toJSObj()](#markdown-header-ssotokendatatojsobj-object) ⇒ Object
3636
* [.toJSObjPretty()](#markdown-header-ssotokendatatojsobjpretty-object) ⇒ Object
3737
* [._getClaim(claimName)](#markdown-header-ssotokendata_getclaimclaimname-stringnumbernull) ⇒ String ⎮ number ⎮ null
38+
* [.getBranchId()](#markdown-header-ssotokendatagetbranchid-nullstring) ⇒ null ⎮ string
39+
* [.getBranchSlug()](#markdown-header-ssotokendatagetbranchslug-nullstring) ⇒ null ⎮ string
3840
* [.getAudience()](#markdown-header-ssotokendatagetaudience-nullstring) ⇒ null ⎮ string
3941
* [.getExpireAtTime()](#markdown-header-ssotokendatagetexpireattime-number) ⇒ number
4042
* [.getNotBeforeTime()](#markdown-header-ssotokendatagetnotbeforetime-number) ⇒ number
@@ -110,6 +112,15 @@ Internally used to get value against the client param string.
110112
| --- | --- | --- |
111113
| claimName | String | The claim name as defined in the tokenDataConsts |
112114

115+
**Kind**: instance method of [SSOTokenData](#markdown-header-new-ssotokendatatokenvals)
116+
### ssoTokenData.getBranchId() ⇒ null ⎮ string
117+
Get the branch ID for which the token was issued.
118+
119+
**Kind**: instance method of [SSOTokenData](#markdown-header-new-ssotokendatatokenvals)
120+
### ssoTokenData.getBranchSlug() ⇒ null ⎮ string
121+
Get the branch slug for which the token was issued.
122+
123+
**Kind**: instance method of [SSOTokenData](#markdown-header-new-ssotokendatatokenvals)
113124
### ssoTokenData.getAudience() ⇒ null ⎮ string
114125
Get targeted audience of the token.
115126

src/lib/SSOToken.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class SSOToken {
7777
}
7878

7979
const tokenDataInst = new TokenData({
80+
CLAIM_BRANCH_ID: decoded.branch_id || null,
81+
CLAIM_BRANCH_SLUG: decoded.branch_slug || null,
8082
CLAIM_AUDIENCE: decoded.aud || null,
8183
CLAIM_EXPIRE_AT: decoded.exp || null,
8284
CLAIM_NOT_BEFORE: decoded.nbf || null,

src/lib/SSOTokenData.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class SSOTokenData {
1212
* @param {Object} tokenVals TokenVals object wit keys representing possible SSO token values.
1313
*/
1414
constructor(tokenVals) {
15+
this.branch_id = tokenVals.CLAIM_BRANCH_ID;
16+
this.branch_slug = tokenVals.CLAIM_BRANCH_SLUG;
1517
this.aud = tokenVals.CLAIM_AUDIENCE;
1618
this.exp = tokenVals.CLAIM_EXPIRE_AT;
1719
this.nbf = tokenVals.CLAIM_NOT_BEFORE;
@@ -106,6 +108,8 @@ class SSOTokenData {
106108
*/
107109
toJSObj() {
108110
return {
111+
branch_id: this.branch_id,
112+
branch_slug: this.branch_slug,
109113
aud: this.aud,
110114
exp: this.exp,
111115
nbf: this.nbf,
@@ -134,6 +138,8 @@ class SSOTokenData {
134138
*/
135139
toJSObjPretty() {
136140
return {
141+
CLAIM_BRANCH_ID: this.branch_id,
142+
CLAIM_BRANCH_SLUG: this.branch_slug,
137143
CLAIM_AUDIENCE: this.aud,
138144
CLAIM_EXPIRE_AT: this.exp,
139145
CLAIM_NOT_BEFORE: this.nbf,
@@ -167,6 +173,24 @@ class SSOTokenData {
167173
}
168174
return this[TokenDataConsts[claimName]] || null;
169175
}
176+
/**
177+
* Get the branch ID for which the token was issued.
178+
*
179+
* @return {null|string}
180+
*/
181+
getBranchId() {
182+
return this._getClaim('CLAIM_BRANCH_ID');
183+
}
184+
185+
/**
186+
* Get the branch slug for which the token was issued.
187+
*
188+
* @return {null|string}
189+
*/
190+
getBranchSlug() {
191+
return this._getClaim('CLAIM_BRANCH_SLUG');
192+
}
193+
170194
/**
171195
* Get targeted audience of the token.
172196
*

src/tests/SSOToken.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const correctAudience = 'testPlugin';
1717
const wrongAudience = 'wrongIssuer';
1818

1919
const tokenDataVals = {
20+
CLAIM_BRANCH_ID: '5e3bfa789f436c5e2ee5141a',
21+
CLAIM_BRANCH_SLUG: 'staffbase',
2022
CLAIM_AUDIENCE: 'testPlugin',
2123
CLAIM_EXPIRE_AT: expTime,
2224
CLAIM_NOT_BEFORE: notBeforeTime,
@@ -146,6 +148,8 @@ describe('Testing SSOToken Class', () => {
146148
test('test token constructor with token data correctly decoded with all values', () => {
147149
const newToken = new SSOToken(correctAudience, keyTokenPub, encodedTokenWithKey);
148150
const expected = {
151+
branch_id: '5e3bfa789f436c5e2ee5141a',
152+
branch_slug: 'staffbase',
149153
aud: 'testPlugin',
150154
exp: expTime,
151155
instance_id: '55c79b6ee4b06c6fb19bd1e2',

src/tests/SSOTokenData.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const notBeforeTime = curTime - (1000 * 60);
99
const secretPub = fs.readFileSync(path.join(__dirname, '../../testKeyFiles/jwtRS256.key')).toString();
1010

1111
const tokenDataVals = {
12+
CLAIM_BRANCH_ID: '5e3bfa789f436c5e2ee5141a',
13+
CLAIM_BRANCH_SLUG: 'staffbase',
1214
CLAIM_AUDIENCE: 'testPlugin',
1315
CLAIM_EXPIRE_AT: expTime,
1416
CLAIM_NOT_BEFORE: notBeforeTime,

src/utils/tokenDataConsts.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const constants = {
2+
CLAIM_BRANCH_ID: 'branch_id',
3+
CLAIM_BRANCH_SLUG: 'branch_slug',
24
CLAIM_AUDIENCE: 'aud',
35
CLAIM_EXPIRE_AT: 'exp',
46
CLAIM_NOT_BEFORE: 'nbf',

0 commit comments

Comments
 (0)