Skip to content

Commit

Permalink
feat(plugin-auth): added plugin auth
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanMatthias committed Dec 12, 2018
1 parent 4d32ab8 commit 65762b9
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/origami/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
"dependencies": {
"@origami/bird": "0.0.3-alpha.7",
"@origami/core": "0.0.3-alpha.7",
"@origami/plugin-auth": "0.0.0",
"@origami/plugin-core-api": "0.0.3-alpha.7",
"@origami/plugin-default-pages": "0.0.3-alpha.7",
"@origami/plugin-setup": "0.0.3-alpha.7",
"@origami/tslint-config": "0.0.3-alpha.3",
"origami-admin-zen": "^0.0.10-alpha.8",
"origami-core-server": "^0.5.4",
"origami-plugin-auth": "^1.0.2",
"origami-plugin-favicon": "^0.0.1",
"origami-plugin-media": "^2.0.0-alpha.2",
"origami-plugin-media": "^2.0.0-alpha.3",
"origami-plugin-user-profiles": "^1.0.1",
"signal-exit": "^3.0.2"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-auth/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src
tsconfig.json
tslint.json
24 changes: 24 additions & 0 deletions packages/plugin-auth/README.md
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
}
...
}
```
19 changes: 19 additions & 0 deletions packages/plugin-auth/package.json
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"
}
}
43 changes: 43 additions & 0 deletions packages/plugin-auth/src/index.ts
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;
8 changes: 8 additions & 0 deletions packages/plugin-auth/tsconfig.json
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"]
}
4 changes: 4 additions & 0 deletions packages/plugin-auth/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@origami/tslint-config",
"defaultSeverity": "error"
}
7 changes: 4 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6330,10 +6330,11 @@ origami-plugin-favicon@^0.0.1:
serve-favicon "^2.5.0"
to-ico "^1.1.5"

origami-plugin-media@^2.0.0-alpha.2:
version "2.0.0-alpha.2"
resolved "https://registry.yarnpkg.com/origami-plugin-media/-/origami-plugin-media-2.0.0-alpha.2.tgz#e6b93aa7ffda4a4c8f6d5ed29b45f4bd8d7dd99c"
origami-plugin-media@^2.0.0-alpha.3:
version "2.0.0-alpha.3"
resolved "https://registry.yarnpkg.com/origami-plugin-media/-/origami-plugin-media-2.0.0-alpha.3.tgz#23c57974f350adc4969e187ee1cbe64216cec17c"
dependencies:
"@origami/core" "^0.0.3-alpha.6"
aws-sdk "^2.337.0"
mkdir-recursive "^0.4.0"

Expand Down

0 comments on commit 65762b9

Please sign in to comment.