-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update api key guard logic (#233)
- Loading branch information
1 parent
a646684
commit 984c33e
Showing
15 changed files
with
203 additions
and
177 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,7 @@ This sections lists notification related requests such as creating new notificat | |
|
||
### Create Notification | ||
|
||
Allows the user to create a new notification for processing and sending it. Requires passing bearer token for authorization. | ||
Allows the user to create a new notification for processing and sending it. Requires passing `x-api-key` token as header for validation. | ||
|
||
**Note:** | ||
- The **Provider** should have a valid `channelType`. | ||
|
@@ -110,7 +110,7 @@ Refer the [Available Channel Types](./usage-guide.md#5-available-channel-types) | |
```sh | ||
curl --location 'http://localhost:3000/notifications' \ | ||
--header 'Content-Type: application/json' \ | ||
--header 'Authorization: Bearer mysecuretoken' \ | ||
--header 'x-api-key: mysecuretoken' \ | ||
--data-raw '{ | ||
"providerId": 1, | ||
"data": { | ||
|
@@ -306,61 +306,6 @@ curl --location 'http://localhost:3000/graphql' \ | |
} | ||
``` | ||
|
||
### Fetch Notification by Id | ||
|
||
Allows the user to fetch notification based on the passed notificationId. | ||
|
||
**Endpoint:** `http://localhost:3000//notifications/{notificationId}` | ||
|
||
**Method:** `GET` | ||
|
||
**Sample Request:** http://localhost:3000/notifications/2 | ||
|
||
**Sample Response:** | ||
|
||
```json | ||
{ | ||
"status": "success", | ||
"data": { | ||
"notification": { | ||
"id": 2, | ||
"providerId": 2, | ||
"channelType": 2, | ||
"data": { | ||
"from": "[email protected]", | ||
"to": "[email protected]", | ||
"subject": "Test subject", | ||
"text": "This is a test notification", | ||
"html": "<b>This is a test notification</b>" | ||
}, | ||
"deliveryStatus": 4, | ||
"result": { | ||
"result": { | ||
"accepted": ["[email protected]"], | ||
"rejected": [], | ||
"ehlo": ["PIPELINING", "8BITMIME", "SMTPUTF8", "AUTH LOGIN PLAIN"], | ||
"envelopeTime": 514, | ||
"messageTime": 396, | ||
"messageSize": 598, | ||
"response": "250 Accepted [STATUS=new MSGID=ZO8BDrs4Cney.EXBZcnPdyNjH.7avN-FAAAAmpIr4f.gj7e4YPfAABUQxYg]", | ||
"envelope": { | ||
"from": "[email protected]", | ||
"to": ["[email protected]"] | ||
}, | ||
"messageId": "<[email protected]>" | ||
} | ||
}, | ||
"createdOn": "2024-04-29T08:14:28.000Z", | ||
"updatedOn": "2024-04-29T08:14:28.000Z", | ||
"createdBy": "sampleFoundationXApp", | ||
"updatedBy": "sampleFoundationXApp", | ||
"status": 1, | ||
"applicationId": 2 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Applications | ||
|
||
This sections lists application related requests such as creating new application and fetching all applications. | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
Injectable, | ||
Logger, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { GqlExecutionContext } from '@nestjs/graphql'; | ||
import { Observable } from 'rxjs'; | ||
import { ServerApiKeysService } from 'src/modules/server-api-keys/server-api-keys.service'; | ||
|
||
@Injectable() | ||
export class AuthGuard implements CanActivate { | ||
constructor( | ||
private readonly serverApiKeysService: ServerApiKeysService, | ||
private logger: Logger, | ||
) {} | ||
|
||
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> { | ||
return this.validateRequest(context); | ||
} | ||
|
||
async validateRequest(execContext: ExecutionContext): Promise<boolean> { | ||
const request = execContext.switchToHttp().getRequest(); | ||
|
||
// Get auth header incase of http request | ||
if (request && request.headers) { | ||
const authHeader = request.headers['authorization']; | ||
const validationResult = await this.validateAuthHeader(authHeader); | ||
|
||
if (validationResult) { | ||
return true; | ||
} | ||
} | ||
|
||
// Get auth header incase of graphql request | ||
const ctx = GqlExecutionContext.create(execContext); | ||
const req = ctx.getContext().req; | ||
const authHeader = req.headers.authorization; | ||
const validationResult = await this.validateAuthHeader(authHeader); | ||
|
||
if (validationResult) { | ||
return true; | ||
} | ||
|
||
throw new UnauthorizedException('Invalid API key'); | ||
} | ||
|
||
// TODO: validate using jwt token instead of db | ||
async validateAuthHeader(authHeader: string): Promise<boolean> { | ||
if (!authHeader) { | ||
this.logger.error('No bearer token provided'); | ||
throw new UnauthorizedException('No bearer token provided'); | ||
} | ||
|
||
let apiKeyToken = null; | ||
|
||
if (authHeader.startsWith('Bearer ')) { | ||
apiKeyToken = authHeader.substring(7); | ||
} else { | ||
this.logger.error('Invalid bearer token format'); | ||
throw new UnauthorizedException('Invalid bearer token format'); | ||
} | ||
|
||
const apiKeyEntry = await this.serverApiKeysService.findByServerApiKey(apiKeyToken); | ||
|
||
if (!apiKeyEntry) { | ||
this.logger.error('Invalid token'); | ||
throw new UnauthorizedException('Invalid token'); | ||
} | ||
|
||
if (apiKeyToken && apiKeyToken === apiKeyEntry.apiKey) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.