diff --git a/backend/README.md b/backend/README.md index 8bf32069..e927a7f4 100644 --- a/backend/README.md +++ b/backend/README.md @@ -18,9 +18,9 @@ The backend for nr-old-growth project is written in [NestJS](https://github.com/ CHES_CLIENT_ID=[CHES service dev username] CHES_CLIENT_SECRET=[CHES service dev password] - // CHES dev authentication url + # CHES dev authentication url CHES_TOKEN_URL=https://dev.oidc.gov.bc.ca/auth/realms/jbd6rnxw/protocol/openid-connect/token - // CHES dev email url + # CHES dev email url EMAIL_POST_URL=https://ches-dev.apps.silver.devops.gov.bc.ca/api/v1/email CHES_EMAIL_FROM=[send from email address] diff --git a/backend/package-lock.json b/backend/package-lock.json index d5249432..a904594b 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -19,8 +19,7 @@ "@nestjs/schedule": "^3.0.0", "@nestjs/swagger": "^7.0.0", "@nestjs/typeorm": "^10.0.0", - "axios": "^1.2.0-alpha.1", - "axios-oauth-client": "^1.5.0", + "axios": "^1.4.0", "body-parser": "^1.20.0", "dotenv": "^16.0.3", "pg": "^8.9.0", @@ -3036,26 +3035,15 @@ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "node_modules/axios": { - "version": "1.3.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.3.4.tgz", - "integrity": "sha512-toYm+Bsyl6VC5wSkfkbbNB6ROv7KY93PEBBL6xyDczaIHasAiv4wPqQ/c4RjoQzipxRD2W5g21cOqQulZ7rHwQ==", + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz", + "integrity": "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==", "dependencies": { "follow-redirects": "^1.15.0", "form-data": "^4.0.0", "proxy-from-env": "^1.1.0" } }, - "node_modules/axios-oauth-client": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/axios-oauth-client/-/axios-oauth-client-1.5.0.tgz", - "integrity": "sha512-CFuTfK9KdRnDDR6LQlUJ0GNKUZ3tHRSFdKPM9WqeCtUdcuKDgWt9aDFH7Xl87VpUcfNt5qRVl4iHdayqtXVv7g==", - "dependencies": { - "qs": "^6.10.1" - }, - "engines": { - "node": ">=10" - } - }, "node_modules/babel-jest": { "version": "29.5.0", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.5.0.tgz", diff --git a/backend/package.json b/backend/package.json index 88b1b0e5..82d141f3 100644 --- a/backend/package.json +++ b/backend/package.json @@ -31,8 +31,7 @@ "@nestjs/schedule": "^3.0.0", "@nestjs/swagger": "^7.0.0", "@nestjs/typeorm": "^10.0.0", - "axios": "^1.2.0-alpha.1", - "axios-oauth-client": "^1.5.0", + "axios": "^1.4.0", "body-parser": "^1.20.0", "dotenv": "^16.0.3", "pg": "^8.9.0", @@ -67,8 +66,8 @@ "@nestjs/schematics": "^9.0.3", "@nestjs/testing": "^9.1.4", "@types/cron": "^2.0.0", - "@types/jest": "^29.4.0", "@types/express": "^4.17.17", + "@types/jest": "^29.4.0", "@types/node": "^18.14.1", "@types/supertest": "^2.0.12", "@typescript-eslint/eslint-plugin": "^5.54.1", diff --git a/backend/src/app.controller.ts b/backend/src/app.controller.ts index f0998ebc..d255eaec 100644 --- a/backend/src/app.controller.ts +++ b/backend/src/app.controller.ts @@ -3,7 +3,7 @@ import { AppService } from './app.service'; @Controller() export class AppController { - constructor(private readonly appService: AppService) { } + constructor(private readonly appService: AppService) {} @Get() getHello(): string { @@ -11,7 +11,7 @@ export class AppController { } @Get('check') - getCheck(): string { + getCheck(): Promise { return this.appService.getToken(); } } diff --git a/backend/src/ches/services/ches.service.ts b/backend/src/ches/services/ches.service.ts index ff3b0693..d923e0c7 100644 --- a/backend/src/ches/services/ches.service.ts +++ b/backend/src/ches/services/ches.service.ts @@ -1,24 +1,26 @@ import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import axios from 'axios'; -import { EmailEntity } from '../../email/model/email.entity'; -const oauth = require('axios-oauth-client'); +import { EmailEntity } from '../../email/model/email.entity'; @Injectable() export class ChesService { getToken() { - const getClientCredentials = oauth.client(axios.create(), { - url: process.env.CHES_TOKEN_URL, - grant_type: 'client_credentials', - client_id: process.env.CHES_CLIENT_ID, - client_secret: process.env.CHES_CLIENT_SECRET, - }); - - return getClientCredentials() - .then((res) => { - if (res && res.access_token) return res.access_token; - else return null; + return axios + .request({ + method: 'POST', + url: process.env.CHES_TOKEN_URL, + auth: { + username: process.env.CHES_CLIENT_ID, + password: process.env.CHES_CLIENT_SECRET, + }, + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + data: 'grant_type=client_credentials', }) + .then((res) => res.data) + .then((token) => token?.access_token) .catch((e) => { throw new HttpException( { message: 'Failed to get email auth token from API: ' + e }, @@ -41,8 +43,11 @@ export class ChesService { !process.env.CHES_EMAIL_FROM ) { throw new HttpException( - { message: 'Failed to config email, server side missing config of authentication url' + - 'or CHES email server url or from email address' }, + { + message: + 'Failed to config email, server side missing config of authentication url' + + 'or CHES email server url or from email address', + }, HttpStatus.BAD_REQUEST, ); } @@ -97,14 +102,17 @@ export class ChesService { } } throw new HttpException( - { message: 'Failed to get email auth token: response or response access token is null' }, + { + message: + 'Failed to get email auth token: response or response access token is null', + }, HttpStatus.BAD_REQUEST, ); }) .catch((e) => { throw new HttpException( - { message: e }, - HttpStatus.INTERNAL_SERVER_ERROR + { message: e }, + HttpStatus.INTERNAL_SERVER_ERROR, ); }); } diff --git a/backend/src/form/services/form.service.ts b/backend/src/form/services/form.service.ts index 809ed3bc..d994522d 100644 --- a/backend/src/form/services/form.service.ts +++ b/backend/src/form/services/form.service.ts @@ -9,8 +9,6 @@ import { EmailService } from '../../email/services/email.service'; import { EmailEntity } from '../../email/model/email.entity'; import { from } from 'rxjs'; -const oauth = require('axios-oauth-client'); - @Injectable() export class FormService { private readonly logger = new Logger(FormService.name); @@ -395,7 +393,7 @@ export class FormService { } }) .catch((e) => { - const errorMsg = `Failed to get submissions list need notification from API: ${e}`; + const errorMsg = 'Failed to get submissions list need notification from API: ' + e; const emailSubmissionLogEntity: EmailSubmissionLog = { code: 'FAILED', exceptionLog: errorMsg,