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

feat: showcase dynamic derived data with SDK #109

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ SCOPES="openid myinfo.name myinfo.passport_expiry_date myinfo.nric_number"
DEV_AND_STAGING_SCOPES="openid sgidthirdpartymock.work_email sgidthirdpartymock.is_public_officer myinfo.nric_number myinfo.name myinfo.email myinfo.mobile_number"
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----woot woot-----END RSA PRIVATE KEY-----"
OVERRIDE_DEV="http://localhost:3000"
RULES_ENGINE_DEV_ENDPOINT="http://localhost:3001/api/rules/eval"
SGID_RULE_IDS="ISOGPOFFICER-53270fce ISOGPOFFICER-aed06721"

1 change: 1 addition & 0 deletions config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export const PORT = process.env.PORT || 10000
export const SCOPES = process.env.SCOPES || 'openid'
export const DEV_AND_STAGING_SCOPES =
process.env.DEV_AND_STAGING_SCOPES || 'openid'
export const SGID_RULE_IDS = process.env.SGID_RULE_IDS || ''
19 changes: 15 additions & 4 deletions routes/callback.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import express from 'express'
import { sgidService } from '../services/sgid-client.service'
import { formatData } from '../utils'
import { formatData, prettifyRuleName } from '../utils'
import { nodeCache } from '../services/node-cache.service'
import { SESSION_COOKIE_NAME } from '../constants'
import { IAuthSession } from '../types'
import { SGID_RULE_IDS } from '../config'

/**
* Main controller function to generate the callback page
Expand All @@ -21,11 +22,21 @@ export const callback = async (req: express.Request, res: express.Response) => {
String(sessionData?.codeVerifier)
)

const { data } = await sgidService[String(state)].userinfo(accessToken, sub)
const formattedData = formatData(data)
const { data: userInfoData } = await sgidService[String(state)].userinfo(
accessToken,
sub
)
const formattedUserInfoData = formatData(userInfoData)

const rulesData = await sgidService[String(state)].rules({
accessToken,
ruleIds: SGID_RULE_IDS,
userInfoData,
})
const formattedRulesData = rulesData.map(data => [prettifyRuleName(data.ruleId), data.output])

res.render('callback', {
data: [['sgID', sub], ...formattedData],
data: [['sgID', sub], ...formattedUserInfoData, ...formattedRulesData],
})
} catch (error) {
console.error(error)
Expand Down
27 changes: 26 additions & 1 deletion services/sgid-client.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import SgidClient, { generatePkcePair } from '@opengovsg/sgid-client'
import SgidClient, {
RulesParams,
RulesReturn,
} from '@opengovsg/sgid-client'
import { BASE_URLS } from '../config'

interface SgidServiceOption {
Expand All @@ -7,25 +10,30 @@ interface SgidServiceOption {
privateKey: string
redirectUri: string
hostname: string
rulesEngineEndpoint: string | undefined
}

class SgidService {
private sgidClient: SgidClient
clientId: string

constructor({
clientId,
clientSecret,
privateKey,
redirectUri,
hostname,
rulesEngineEndpoint
}: SgidServiceOption) {
this.sgidClient = new SgidClient({
clientId: clientId,
clientSecret: clientSecret,
privateKey: privateKey,
redirectUri: redirectUri,
hostname: hostname,
rulesEngineEndpoint: rulesEngineEndpoint
})
this.clientId = clientId
}

/**
Expand Down Expand Up @@ -80,6 +88,22 @@ class SgidService {
throw new Error('Error retrieving user info via sgid-client')
}
}

async rules(rulesParams: RulesParams): Promise<RulesReturn> {
const { accessToken, ruleIds, userInfoData } = rulesParams

if (!accessToken) throw new Error(`accessToken cannot be empty`)
if (!ruleIds) throw new Error(`ruleIds cannot be empty`)
if (!userInfoData) throw new Error(`userInfoData cannot be empty`)

try {
const data = await this.sgidClient.rules(rulesParams)
return data
} catch (e) {
console.error(e)
throw new Error('Error retrieving rule-based fields')
}
}
}

// Initialised the sgidService object with the different environments
Expand All @@ -93,5 +117,6 @@ Object.keys(BASE_URLS).forEach((env) => {
privateKey: process.env.PRIVATE_KEY as string,
redirectUri: process.env.HOSTNAME + '/callback',
hostname: BASE_URLS[env as keyof typeof BASE_URLS] as string,
rulesEngineEndpoint: env === 'dev' ? process.env.RULES_ENGINE_DEV_ENDPOINT : undefined
})
})
4 changes: 4 additions & 0 deletions utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ export const prettifyKey = (key: string): string => {
prettified = prettified.replace(/_/g, ' ')
return prettified.toUpperCase()
}

export const prettifyRuleName = (ruleName: string): string => {
return ruleName.replace(/_/g, ' ').toUpperCase()
}