Skip to content

Commit

Permalink
refector: Digilocker url added in constant file
Browse files Browse the repository at this point in the history
Signed-off-by: tusharbhayani <tushar.bhayani@ayanworks.com>
  • Loading branch information
tusharbhayani committed Nov 11, 2024
1 parent c32fc3d commit c377dc8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
10 changes: 10 additions & 0 deletions packages/digilocker/src/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const DIGILOCKER_TOKEN_URL = 'https://api.digitallocker.gov.in/public/oauth2/1/token'
export const DIGILOCKER_AADHAAR = 'https://api.digitallocker.gov.in/public/oauth2/3/xml/eaadhaar'
export const DIGILOCKER_ISSUE_DOCUMENT = 'https://api.digitallocker.gov.in/public/oauth2/2/files/issued'
export const DIGILOCKER_FETCH_DOCUMENT = 'https://api.digitallocker.gov.in/public/oauth2/1/xml/'
export const DIGILOCKER_FETCH_FILE = 'https://api.digitallocker.gov.in/public/oauth2/1/file/'
export const DIGILOCKER_CLIENT_ID_URL_1 =
'https://api.digitallocker.gov.in/public/oauth2/1/authorize?response_type=code&client_id='
export const DIGILOCKER_REDIRECT_URL_2 = '&redirect_uri='
export const DIGILOCKER_CODE_CHALLENGE_URL_3 = '&state=adeya2024&code_challenge='
export const DIGILOCKER_CODE_CHALLENGE_METHOD_URL_4 = '&code_challenge_method=S256'
30 changes: 18 additions & 12 deletions packages/digilocker/src/digilocker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import axios from 'axios'
import { createHash } from 'crypto'

import {
DIGILOCKER_AADHAAR,
DIGILOCKER_CLIENT_ID_URL_1,
DIGILOCKER_CODE_CHALLENGE_METHOD_URL_4,
DIGILOCKER_CODE_CHALLENGE_URL_3,
DIGILOCKER_FETCH_DOCUMENT,
DIGILOCKER_FETCH_FILE,
DIGILOCKER_ISSUE_DOCUMENT,
DIGILOCKER_REDIRECT_URL_2,
DIGILOCKER_TOKEN_URL
} from './constant'

export type AdeyaDigiLockerModuleOptions = {
client_id?: string | undefined
client_secret?: string | undefined
Expand All @@ -25,7 +37,7 @@ export const initiateDigiLockerOAuth = async ({
}: AdeyaDigiLockerModuleOptions) => {
try {
const codeChallenge = generateCodeChallenge(codeVerifier)
const authUrl = `https://api.digitallocker.gov.in/public/oauth2/1/authorize?response_type=code&client_id=${client_id}&redirect_uri=${redirect_url}&state=adeya2024&code_challenge=${codeChallenge}&code_challenge_method=S256`
const authUrl = `${DIGILOCKER_CLIENT_ID_URL_1}${client_id}${DIGILOCKER_REDIRECT_URL_2}${redirect_url}${DIGILOCKER_CODE_CHALLENGE_URL_3}${codeChallenge}${DIGILOCKER_CODE_CHALLENGE_METHOD_URL_4}`
return authUrl
} catch (error) {
return error instanceof Error ? error : new Error('An unknown error occurred')
Expand All @@ -39,8 +51,6 @@ export const fetchDigiLockerToken = async ({
redirect_url = '',
codeVerifier = ''
}: AdeyaDigiLockerModuleOptions) => {
const tokenUrl = 'https://api.digitallocker.gov.in/public/oauth2/1/token'

const params =
`grant_type=authorization_code&` +
`code=${encodeURIComponent(authCode)}&` +
Expand All @@ -50,7 +60,7 @@ export const fetchDigiLockerToken = async ({
`code_verifier=${encodeURIComponent(codeVerifier)}`

try {
const response = await axios.post(tokenUrl, params, {
const response = await axios.post(DIGILOCKER_TOKEN_URL, params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
Expand All @@ -63,10 +73,8 @@ export const fetchDigiLockerToken = async ({
}

export const fetchAadhaarData = async (accessToken: string): Promise<{ message: string }> => {
const aadhaarUrl = 'https://api.digitallocker.gov.in/public/oauth2/3/xml/eaadhaar'

try {
const response = await axios.get(aadhaarUrl, {
const response = await axios.get(DIGILOCKER_AADHAAR, {
headers: {
Authorization: `Bearer ${accessToken}`
}
Expand All @@ -79,10 +87,8 @@ export const fetchAadhaarData = async (accessToken: string): Promise<{ message:
}

export const fetchIssuedDocuments = async (accessToken: string): Promise<{ message: string }> => {
const issuedDocumentsUrl = 'https://api.digitallocker.gov.in/public/oauth2/2/files/issued'

try {
const response = await axios.get(issuedDocumentsUrl, {
const response = await axios.get(DIGILOCKER_ISSUE_DOCUMENT, {
headers: {
Authorization: `Bearer ${accessToken}`
}
Expand All @@ -95,7 +101,7 @@ export const fetchIssuedDocuments = async (accessToken: string): Promise<{ messa
}

export const fetchDocumentData = async (uri: string, accessToken: string): Promise<{ message: string }> => {
const documentUrl = `https://api.digitallocker.gov.in/public/oauth2/1/xml/${uri}`
const documentUrl = `${DIGILOCKER_FETCH_DOCUMENT}${uri}`

try {
const response = await axios.get(documentUrl, {
Expand All @@ -111,7 +117,7 @@ export const fetchDocumentData = async (uri: string, accessToken: string): Promi
}

export const fetchDocument = async (uri: string, accessToken: string): Promise<{ message: string }> => {
const documentUrl = `https://api.digitallocker.gov.in/public/oauth2/1/file/${uri}`
const documentUrl = `${DIGILOCKER_FETCH_FILE}${uri}`

try {
const response = await axios.get(documentUrl, {
Expand Down

0 comments on commit c377dc8

Please sign in to comment.