Skip to content

Commit

Permalink
feat: add more sgid scopes (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
PrawiraGenestonlia authored Oct 9, 2023
1 parent bbfa1ca commit 4fea33a
Showing 1 changed file with 66 additions and 8 deletions.
74 changes: 66 additions & 8 deletions lib/express/sgid.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ function config(app, { showLoginPage, serviceProvider }) {
'myinfo.residential',
'myinfo.housingtype',
'myinfo.hdbtype',
'myinfo.birth_country',
'myinfo.vehicles',
'myinfo.name_of_employer',
'myinfo.workpass_status',
'myinfo.workpass_expiry_date',
'myinfo.marital_status',
'myinfo.mobile_number_with_country_code',
],
id_token_signing_alg_values_supported: ['RS256'],
subject_types_supported: ['pairwise'],
Expand All @@ -232,28 +239,79 @@ const concatMyInfoRegAddr = (regadd) => {
return `${line1}\n${line2}\n${line3}`
}

// Refer to sgid myinfo parser
const formatMobileNumberWithPrefix = (phone) => {
if (!phone || !phone.nbr?.value) {
return 'NA'
}
return phone.prefix?.value && phone.areacode?.value
? `${phone.prefix?.value}${phone.areacode?.value} ${phone.nbr?.value}`
: phone.nbr?.value
}

// Refer to sgid myinfo parser
const formatVehicles = (vehicles) => {
const vehicleObjects =
vehicles?.map((vehicle) => ({
vehicle_number: vehicle.vehicleno?.value || 'NA',
})) || '[]'
return vehicleObjects
}

const defaultUndefinedToNA = (value) => {
return value || 'NA'
}

// Refer to https://docs.id.gov.sg/data-catalog
const sgIDScopeToMyInfoField = (persona, scope) => {
switch (scope) {
// No NRIC as that is always returned by default
case 'openid':
return persona.uuid.value
return defaultUndefinedToNA(persona.uuid?.value)
case 'myinfo.name':
return persona.name.value
return defaultUndefinedToNA(persona.name?.value)
case 'myinfo.email':
return persona.email.value
return defaultUndefinedToNA(persona.email?.value)
case 'myinfo.sex':
return defaultUndefinedToNA(persona.sex?.desc)
case 'myinfo.race':
return defaultUndefinedToNA(persona.race?.desc)
case 'myinfo.mobile_number':
return persona.mobileno.nbr.value
return defaultUndefinedToNA(persona.mobileno?.nbr?.value)
case 'myinfo.registered_address':
return concatMyInfoRegAddr(persona.regadd)
case 'myinfo.date_of_birth':
return persona.dob.value
return defaultUndefinedToNA(persona.dob?.value)
case 'myinfo.passport_number':
return persona.passportnumber.value
return defaultUndefinedToNA(persona.passportnumber?.value)
case 'myinfo.passport_expiry_date':
return persona.passportexpirydate.value
return defaultUndefinedToNA(persona.passportexpirydate?.value)
case 'myinfo.nationality':
return defaultUndefinedToNA(persona.nationality?.desc)
case 'myinfo.residentialstatus':
return defaultUndefinedToNA(persona.residentialstatus?.desc)
case 'myinfo.residential':
return defaultUndefinedToNA(persona.residential?.desc)
case 'myinfo.housingtype':
return defaultUndefinedToNA(persona.housingtype?.desc)
case 'myinfo.hdbtype':
return defaultUndefinedToNA(persona.hdbtype?.desc)
case 'myinfo.birth_country':
return defaultUndefinedToNA(persona.birthcountry?.desc)
case 'myinfo.vehicles':
return formatVehicles(persona.vehicles)
case 'myinfo.name_of_employer':
return defaultUndefinedToNA(persona.employment?.value)
case 'myinfo.workpass_status':
return defaultUndefinedToNA(persona.passstatus?.value)
case 'myinfo.workpass_expiry_date':
return defaultUndefinedToNA(persona.passexpirydate?.value)
case 'myinfo.marital_status':
return defaultUndefinedToNA(persona.marital?.desc)
case 'myinfo.mobile_number_with_country_code':
return formatMobileNumberWithPrefix(persona.mobileno)
default:
return ''
return 'NA'
}
}

Expand Down

0 comments on commit 4fea33a

Please sign in to comment.