Skip to content

Commit

Permalink
feat(sgid): migrate to v2 (#542)
Browse files Browse the repository at this point in the history
Make a breaking change to sgid, taking advantage of relatively low
uptake, to encourage people to move to sgID v2
  • Loading branch information
LoneRifle authored May 17, 2023
1 parent c3f370a commit 1dcc26e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ MyInfo:
- http://localhost:5156/myinfo/v3/person

sgID:
- http://localhost:5156/sgid/v1/oauth/authorize
- http://localhost:5156/sgid/v1/oauth/token
- http://localhost:5156/sgid/v1/oauth/userinfo
- http://localhost:5156/v2/oauth/authorize
- http://localhost:5156/v2/oauth/token
- http://localhost:5156/v2/oauth/userinfo
- http://localhost:5156/v2/.well-known/openid-configuration - OpenID discovery endpoint
- http://localhost:5156/v2/.well-known/jwks.json - JWKS endpoint which exposes the auth provider's signing keys

Provide your application with the `spcp*` certs found in `static/certs`
and with application certs at `static/certs/{key.pem|server.crt}`
Expand Down
81 changes: 43 additions & 38 deletions lib/express/sgid.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const LOGIN_TEMPLATE = fs.readFileSync(
'utf8',
)

const PATH_PREFIX = '/sgid/v1/oauth'
const VERSION_PREFIX = '/v2'
const OAUTH_PREFIX = '/oauth'
const PATH_PREFIX = VERSION_PREFIX + OAUTH_PREFIX

const signingPem = fs.readFileSync(
path.resolve(__dirname, '../../static/certs/spcp-key.pem'),
Expand Down Expand Up @@ -77,7 +79,7 @@ function config(app, { showLoginPage, serviceProvider }) {
`Profile ${JSON.stringify(profile)} with token scope ${scopes}`,
)
const accessToken = authCode
const iss = `${req.protocol}://${req.get('host')}`
const iss = `${req.protocol}://${req.get('host') + VERSION_PREFIX}`

const { idTokenClaims, refreshToken } = assertions.oidc.create.singPass(
profile,
Expand Down Expand Up @@ -165,49 +167,52 @@ function config(app, { showLoginPage, serviceProvider }) {
})
})

app.get('/.well-known/jwks.json', async (_req, res) => {
app.get(`${VERSION_PREFIX}/.well-known/jwks.json`, async (_req, res) => {
const key = await jose.JWK.asKey(signingPem, 'pem')
const jwk = key.toJSON()
jwk.use = 'sig'
res.json({ keys: [jwk] })
})

app.get('/.well-known/openid-configuration', async (req, res) => {
const issuer = `${req.protocol}://${req.get('host')}`
app.get(
`${VERSION_PREFIX}/.well-known/openid-configuration`,
async (req, res) => {
const issuer = `${req.protocol}://${req.get('host') + VERSION_PREFIX}`

res.json({
issuer,
authorization_endpoint: `${issuer}/${PATH_PREFIX}/authorize`,
token_endpoint: `${issuer}/${PATH_PREFIX}/token`,
userinfo_endpoint: `${issuer}/${PATH_PREFIX}/userinfo`,
jwks_uri: `${issuer}/.well-known/jwks.json`,
response_types_supported: ['code'],
grant_types_supported: ['authorization_code'],
// Note: some of these scopes are not yet officially documented
// in https://docs.id.gov.sg/data-catalog
// So they are not officially supported yet.
scopes_supported: [
'openid',
'myinfo.nric_number',
'myinfo.name',
'myinfo.email',
'myinfo.sex',
'myinfo.race',
'myinfo.mobile_number',
'myinfo.registered_address',
'myinfo.date_of_birth',
'myinfo.passport_number',
'myinfo.passport_expiry_date',
'myinfo.nationality',
'myinfo.residentialstatus',
'myinfo.residential',
'myinfo.housingtype',
'myinfo.hdbtype',
],
id_token_signing_alg_values_supported: ['RS256'],
subject_types_supported: ['pairwise'],
})
})
res.json({
issuer,
authorization_endpoint: `${issuer}/${OAUTH_PREFIX}/authorize`,
token_endpoint: `${issuer}/${OAUTH_PREFIX}/token`,
userinfo_endpoint: `${issuer}/${OAUTH_PREFIX}/userinfo`,
jwks_uri: `${issuer}/.well-known/jwks.json`,
response_types_supported: ['code'],
grant_types_supported: ['authorization_code'],
// Note: some of these scopes are not yet officially documented
// in https://docs.id.gov.sg/data-catalog
// So they are not officially supported yet.
scopes_supported: [
'openid',
'myinfo.nric_number',
'myinfo.name',
'myinfo.email',
'myinfo.sex',
'myinfo.race',
'myinfo.mobile_number',
'myinfo.registered_address',
'myinfo.date_of_birth',
'myinfo.passport_number',
'myinfo.passport_expiry_date',
'myinfo.nationality',
'myinfo.residentialstatus',
'myinfo.residential',
'myinfo.housingtype',
'myinfo.hdbtype',
],
id_token_signing_alg_values_supported: ['RS256'],
subject_types_supported: ['pairwise'],
})
},
)
}

const concatMyInfoRegAddr = (regadd) => {
Expand Down

0 comments on commit 1dcc26e

Please sign in to comment.