-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
93 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Authentication middleware | ||
*/ | ||
|
||
import { ILogger } from './interfaces'; | ||
import * as crypto from 'crypto'; | ||
|
||
export function generateAuthMiddleware(apiKeyHash: string, log: ILogger) { | ||
return (req, res, next) => { | ||
const apiKey = req.get('authorization'); | ||
|
||
// Check if API key was provided | ||
if (!apiKey) { | ||
return res.status(401).json({ error: 'MISSING_APIKEY', message: 'No API key provided via the HTTP \'authorization\' header' }); | ||
} | ||
|
||
// Check if API key is correct | ||
const hash: string = crypto.createHash('sha256').update(apiKey).digest('hex'); | ||
|
||
if (hash !== apiKeyHash) { | ||
return res.status(401).json({ error: 'WRONG_APIKEY', message: 'The provided API key is incorrect'}); | ||
} | ||
|
||
// Authenticated successfully | ||
return next(); | ||
}; | ||
} |
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,26 @@ | ||
|
||
export interface IServerOptions { | ||
/** Server host */ | ||
host?: string; | ||
/** Server port */ | ||
port?: number; | ||
/** Whether the API is disabled by default. Results in the start() method exiting immediately (default: false) */ | ||
disabled?: boolean; | ||
/** Enable authentication (default: false) */ | ||
authentication?: boolean; | ||
/** Provide the SHA256 hash (in hexadecimal format) of the API key that is used for authentication */ | ||
apikeyhash?: string; | ||
/** Namespace for environment variables (default: 'PM2API') */ | ||
envNamespace?: string; | ||
/** Logger */ | ||
logger?: ILogger; | ||
} | ||
|
||
export interface ILogger { | ||
/** Logging function for info messages (default: console.log) */ | ||
info?: Function; | ||
/** Logging function for warnings (default: console.log) */ | ||
warn?: Function; | ||
/** Logging function for errors (default: console.log) */ | ||
err?: Function; | ||
} |