-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): allow issuing single record specific tokens (#7728)
* refactor: move metrics out of authenticate * feat: initial token exchange endpoint * chore: update comment to be more specific * fix: issue with calling metrics environment * chore: amend changelog * refactor: improve clarity * fix: unused import * chore: add missing schema inputs * revert: revert mosip code removal to keep PR contained * chore: add comment about more fine-grained control * chore: fix test on gateway * feat: actually check if the record id matches to confirm record * revert: the confirm registration changes - lets do inanother pr * refactor: update error messages * fix: make the no-op simpler * fix: the query params not passing properly via gateway * refactor: remove unnecessary gql inputs * fix: update audiences to include minimum * fix: update the todo comment
- Loading branch information
Showing
24 changed files
with
316 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
import * as Hapi from '@hapi/hapi' | ||
import { clientCredentialsHandler } from './client-credentials' | ||
import * as oauthResponse from './responses' | ||
import { tokenExchangeHandler } from './token-exchange' | ||
|
||
export async function tokenHandler( | ||
request: Hapi.Request, | ||
h: Hapi.ResponseToolkit | ||
) { | ||
const grantType = request.query.grant_type | ||
|
||
if (grantType === 'client_credentials') { | ||
return clientCredentialsHandler(request, h) | ||
} | ||
|
||
if (grantType === 'urn:opencrvs:oauth:grant-type:token-exchange') { | ||
return tokenExchangeHandler(request, h) | ||
} | ||
|
||
return oauthResponse.invalidGrantType(h) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
import * as Hapi from '@hapi/hapi' | ||
|
||
export const invalidRequest = (h: Hapi.ResponseToolkit) => | ||
h | ||
.response({ | ||
error: 'invalid_request', | ||
error_description: | ||
'Invalid request. Check that all required parameters have been provided.', | ||
error_uri: | ||
'Refer to https://documentation.opencrvs.org/technology/interoperability/authenticate-a-client' | ||
}) | ||
.header('Cache-Control', 'no-store') | ||
.code(400) | ||
|
||
export const invalidGrantType = (h: Hapi.ResponseToolkit) => | ||
h | ||
.response({ | ||
error: 'unsupported_grant_type', | ||
error_description: `Invalid or undefined grant type. Only "client_credentials" or "urn:opencrvs:oauth:grant-type:token-exchange" are supported.`, | ||
error_uri: | ||
'Refer to https://documentation.opencrvs.org/technology/interoperability/authenticate-a-client' | ||
}) | ||
|
||
.header('Cache-Control', 'no-store') | ||
.code(400) | ||
|
||
export const invalidClient = (h: Hapi.ResponseToolkit) => | ||
h | ||
.response({ | ||
error: 'invalid_client', | ||
error_description: 'Invalid client id or secret', | ||
error_uri: | ||
'Refer to https://documentation.opencrvs.org/technology/interoperability/authenticate-a-client' | ||
}) | ||
.header('Cache-Control', 'no-store') | ||
.code(401) | ||
|
||
export const invalidSubjectToken = (h: Hapi.ResponseToolkit) => | ||
h | ||
.response({ | ||
error: 'unauthorized_client', | ||
error_description: 'Invalid subject token' | ||
}) | ||
.header('Cache-Control', 'no-store') | ||
.code(401) | ||
|
||
export const success = (h: Hapi.ResponseToolkit, token: string) => | ||
h | ||
.response({ | ||
access_token: token, | ||
token_type: 'Bearer' | ||
}) | ||
.header('Cache-Control', 'no-store') | ||
.code(200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
import * as oauthResponse from './responses' | ||
import * as Hapi from '@hapi/hapi' | ||
import { | ||
createTokenForRecordValidation, | ||
verifyToken | ||
} from '@auth/features/authenticate/service' | ||
import { pipe } from 'fp-ts/lib/function' | ||
import { UUID } from '@opencrvs/commons' | ||
|
||
export const SUBJECT_TOKEN_TYPE = | ||
'urn:ietf:params:oauth:token-type:access_token' | ||
export const RECORD_TOKEN_TYPE = | ||
'urn:opencrvs:oauth:token-type:single_record_token' | ||
|
||
/** | ||
* Allows creating record-specific tokens for when a 3rd party system needs to confirm a registration | ||
* | ||
* https://datatracker.ietf.org/doc/html/rfc8693#section-2.1 | ||
*/ | ||
export async function tokenExchangeHandler( | ||
request: Hapi.Request, | ||
h: Hapi.ResponseToolkit | ||
) { | ||
const subjectToken = request.query.subject_token | ||
const subjectTokenType = request.query.subject_token_type | ||
const requestedTokenType = request.query.requested_token_type | ||
const recordId = request.query.record_id | ||
|
||
if ( | ||
!recordId || | ||
!subjectToken || | ||
subjectTokenType !== SUBJECT_TOKEN_TYPE || | ||
requestedTokenType !== RECORD_TOKEN_TYPE | ||
) { | ||
return oauthResponse.invalidRequest(h) | ||
} | ||
|
||
const decodedOrError = pipe(subjectToken, verifyToken) | ||
if (decodedOrError._tag === 'Left') { | ||
return oauthResponse.invalidSubjectToken(h) | ||
} | ||
const { sub } = decodedOrError.right | ||
|
||
// @TODO: If in the future we have a fine grained access control for records, check here that the subject actually has access to the record requested | ||
const recordToken = await createTokenForRecordValidation( | ||
sub as UUID, | ||
recordId | ||
) | ||
|
||
return oauthResponse.success(h, recordToken) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.