Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peter/get org update #220

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions src/session/getOrganization.js

This file was deleted.

34 changes: 34 additions & 0 deletions src/session/getOrganization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import jwtDecode from 'jwt-decode';
import {KindeAccessToken, KindeIdToken} from '../../types';
import {config} from '../config/index';
import {generateOrganizationObject} from '../utils/generateOrganizationObject';
import {sessionManager} from './sessionManager';
/**
* @callback getOrganization
* @returns {Promise<import('../../types').KindeOrganization | null>}
*/

/**
*
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns {getOrganization}
*/
export const getOrganizationFactory = (req, res) => async () => {
try {
const idToken = jwtDecode(
(await sessionManager(req, res).getSessionItem('id_token')) as string
) as KindeIdToken;
peterphanouvong marked this conversation as resolved.
Show resolved Hide resolved

const accessToken = jwtDecode(
(await sessionManager(req, res).getSessionItem('access_token')) as string
) as KindeAccessToken;

return generateOrganizationObject(idToken, accessToken);
} catch (error) {
if (config.isDebugMode) {
console.error(error);
}
return null;
}
peterphanouvong marked this conversation as resolved.
Show resolved Hide resolved
};
31 changes: 31 additions & 0 deletions src/utils/generateOrganizationObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {KindeAccessToken, KindeIdToken} from '../../types';

export const generateOrganizationObject = (
idToken: KindeIdToken,
accessToken: KindeAccessToken
) => {
return {
orgCode: accessToken.org_code,
orgName: accessToken.org_name,
properties: {
city:
idToken.organization_properties?.kp_org_city?.v ||
accessToken.organization_properties?.kp_org_city?.v,
industry:
idToken.organization_properties?.kp_org_industry?.v ||
accessToken.organization_properties?.kp_org_industry?.v,
postcode:
idToken.organization_properties?.kp_org_postcode?.v ||
accessToken.organization_properties?.kp_org_postcode?.v,
state_region:
idToken.organization_properties?.kp_org_state_region?.v ||
accessToken.organization_properties?.kp_org_state_region?.v,
street_address:
idToken.organization_properties?.kp_org_street_address?.v ||
accessToken.organization_properties?.kp_org_street_address?.v,
street_address_2:
idToken.organization_properties?.kp_org_street_address_2?.v ||
accessToken.organization_properties?.kp_org_street_address_2?.v
}
};
};
peterphanouvong marked this conversation as resolved.
Show resolved Hide resolved
198 changes: 198 additions & 0 deletions tests/frontend/generate-organization-object.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import {generateOrganizationObject} from '../../src/utils/generateOrganizationObject'; // Assuming the function is exported from utils file
import {KindeAccessToken, KindeIdToken} from '../../types';

describe('generateOrganizationObject', () => {
const orgProperties = {
kp_org_city: {
v: 'Sydney'
},
kp_org_industry: {
v: 'Software'
},
kp_org_postcode: {
v: '2165'
},
kp_org_state_region: {
v: 'NSW'
},
kp_org_street_address: {},
kp_org_street_address_2: {}
};

const accessTokenWithoutProperties: KindeAccessToken = {
aud: [],
azp: '463db732edf24274970e395735e6d3e2',
email: '[email protected]',
exp: 1724626727,
feature_flags: {
boolean: {
t: 'b',
v: true
},
booleanflag: {
t: 'b',
v: true
},
flag: {
t: 'j',
v: {
message: 'hi'
}
},
flagtest: {
t: 'j',
v: {
flag: 'yeeee'
}
},
integerflag: {
t: 'i',
v: 7
},
integerflagtest: {
t: 'i',
v: 123
},
stringflag: {
t: 's',
v: 'asdfg'
},
stringflagtest: {
t: 's',
v: 'stringFlagTest'
},
test: {
t: 'b',
v: true
}
},
iat: 1724626665,
iss: 'https://peter.kinde.com',
jti: 'a39d1f76-f87d-447b-8d37-ae5bfcd36bd2',
org_code: 'org_95755120efb',
org_name: 'Peter Phanouvong',
permissions: ['test2', 'tester'],
roles: [
{
id: '018f17f2-48dc-c606-94a7-0f425996ef57',
key: 'admin',
name: 'Admin'
},
{
id: '018e9bd7-d933-e4f3-dd7d-686e53be8a2c',
key: 'bud',
name: 'Bud'
}
],
scp: ['openid', 'profile', 'email', 'offline'],
sub: 'kp:3b1b9e1c1a5a46bfae4e46c969065381'
};

const accessToken: KindeAccessToken = {
...accessTokenWithoutProperties,
organization_properties: orgProperties
};

const idTokenWithoutProperties: KindeIdToken = {
at_hash: '07zCRWxQSbvxUYvB2KL6tA',
aud: ['463db732edf24274970e395735e6d3e2'],
auth_time: 1724626003,
azp: '463db732edf24274970e395735e6d3e2',
email: '[email protected]',
email_verified: true,
exp: 1724626065,
family_name: 'Phanouvong',
given_name: 'Peterzz',
iat: 1724626004,
iss: 'https://peter.kinde.com',
jti: '4b8f85a5-7996-45cf-811b-29ecdcce9c31',
name: 'Peterzz Phanouvong',
org_codes: ['org_19eb76166dee3', 'org_8615151456b42', 'org_95755120efb'],
organizations: [
{
id: 'org_19eb76166dee3',
name: 'RBAC'
},
{
id: 'org_8615151456b42',
name: 'awesome'
},
{
id: 'org_95755120efb',
name: 'Peter Phanouvong'
}
],
picture:
'https://lh3.googleusercontent.com/a/ACg8ocJy7qVlRTf6YhuE5u6Z1FK30BvfXNK5OoMydpzct5oXFrUDRQ=s96-c',
preferred_username: 'peteswah',
rat: 1724626003,
sub: 'kp:3b1b9e1c1a5a46bfae4e46c969065381',
updated_at: 1724286328
};

const idToken: KindeIdToken = {
...idTokenWithoutProperties,
organization_properties: orgProperties
};

test('should generate org object', () => {
expect(generateOrganizationObject(idToken, accessToken)).toEqual({
orgCode: 'org_95755120efb',
orgName: 'Peter Phanouvong',
properties: {
city: 'Sydney',
industry: 'Software',
postcode: '2165',
state_region: 'NSW'
}
});
});
test('should generate org object when there are no properties in idtoken', () => {
expect(
generateOrganizationObject(idTokenWithoutProperties, accessToken)
).toEqual({
orgCode: 'org_95755120efb',
orgName: 'Peter Phanouvong',
properties: {
city: 'Sydney',
industry: 'Software',
postcode: '2165',
state_region: 'NSW'
}
});
});
test('should generate org object when there are no properties in access token', () => {
expect(
generateOrganizationObject(idToken, accessTokenWithoutProperties)
).toEqual({
orgCode: 'org_95755120efb',
orgName: 'Peter Phanouvong',
properties: {
city: 'Sydney',
industry: 'Software',
postcode: '2165',
state_region: 'NSW'
}
});
});

test('should generate org object with no properties when not properties in access token or id token', () => {
expect(
generateOrganizationObject(
idTokenWithoutProperties,
accessTokenWithoutProperties
)
).toEqual({
orgCode: 'org_95755120efb',
orgName: 'Peter Phanouvong',
properties: {
city: undefined,
industry: undefined,
postcode: undefined,
state_region: undefined,
street_address: undefined,
street_address_2: undefined
}
});
});
});
Loading