-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-auth): added plugin auth
- Loading branch information
1 parent
4d32ab8
commit 65762b9
Showing
8 changed files
with
107 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
src | ||
tsconfig.json | ||
tslint.json |
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,24 @@ | ||
# Origami Default Pages plugin | ||
|
||
This plugin provides fallbacks for default pages such as 404, 500, /index, etc. | ||
It also provides some common styling static resources used across the site like | ||
`base.css`, `logo.svg`, `waves.svg`, etc. | ||
|
||
**This is a default plugin in Origami** | ||
|
||
## Installation | ||
`yarn add origami-plugin-default-pages` | ||
|
||
## Configuration | ||
In your `.origami` file, add this to your `plugins`: | ||
|
||
|
||
```JSON | ||
{ | ||
... | ||
"plugins": { | ||
"default-pages": true | ||
} | ||
... | ||
} | ||
``` |
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,19 @@ | ||
{ | ||
"name": "@origami/plugin-auth", | ||
"version": "0.0.0", | ||
"description": "Authentication plugin for the Origami platform", | ||
"repository": "https://github.com/origami-cms/core/tree/master/packages/plugin-auth", | ||
"homepage": "http://www.origami.so", | ||
"author": "Tristan Matthias <[email protected]>", | ||
"license": "MIT", | ||
"main": "./build/index.js", | ||
"typings": "./build/index.d.ts", | ||
"scripts": { | ||
"clean": "rm -rf build", | ||
"watch": "tsc -w", | ||
"build": "yarn clean && tsc" | ||
}, | ||
"dependencies": { | ||
"@origami/core": "0.0.3-alpha.7" | ||
} | ||
} |
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,43 @@ | ||
import { auth as authLib, Origami, Server } from '@origami/core'; | ||
|
||
// tslint:disable-next-line variable-name | ||
const Auth: Origami.Server.RequestHandler = async (req, res, next) => { | ||
try { | ||
const head = req.headers.authorization; | ||
|
||
const _auth = head as string; | ||
|
||
if (!head) throw new Error('auth.errors.noHeader'); | ||
const jwtRegex: RegExp = /Bearer\s(.+)/; | ||
const regexResult = jwtRegex.exec(_auth); | ||
if (!regexResult) throw new Error('auth.errors.invalidHead'); | ||
const [, jwt] = regexResult; | ||
|
||
let data; | ||
try { | ||
data = authLib.jwtVerify(jwt, res.app.get('secret')); | ||
} catch (e) { | ||
if (e.name === 'JsonWebTokenError') { | ||
throw new Error('auth.errors.invalidJWT'); | ||
} | ||
if (e.name === 'TokenExpiredError') { | ||
throw new Error('auth.errors.expired'); | ||
} | ||
throw e; | ||
} | ||
req.jwt = { | ||
token: jwt, | ||
data | ||
}; | ||
|
||
next(); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}; | ||
|
||
// tslint:disable-next-line no-default-export export-name | ||
export default (server: Server) => { | ||
server.namedMiddleware('auth', Auth); | ||
}; | ||
export const auth = Auth; |
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,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./build", | ||
"rootDir": "./src" | ||
}, | ||
"include": ["./src/**/*.ts"] | ||
} |
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,4 @@ | ||
{ | ||
"extends": "@origami/tslint-config", | ||
"defaultSeverity": "error" | ||
} |
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