-
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.
Added Client Credentials grant with tests
- Loading branch information
yohei.kanehara
committed
May 11, 2017
1 parent
37e98e1
commit 754653e
Showing
26 changed files
with
2,304 additions
and
71 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
lib | ||
mochawesome-reports | ||
.env |
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 @@ | ||
CONFIG_PATH=[absolute path to local-properties.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 |
---|---|---|
|
@@ -14,4 +14,4 @@ COPY . /app | |
|
||
EXPOSE 3000 | ||
|
||
CMD ["npm", "run", "start:prod"] | ||
CMD ["npm", "run", "start:production"] |
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 |
---|---|---|
@@ -1,5 +1,19 @@ | ||
/** | ||
* This file is only executed locally | ||
* Non locally, we will pre-compile src/ with Babel and build it to a lib/ directory and execute that instead | ||
* | ||
* See package.json for details of the scripts | ||
*/ | ||
|
||
const assert = require('assert'); | ||
|
||
assert(process.env.NODE_ENV !== 'production'); | ||
|
||
// Read in environment variables from .env | ||
require('dotenv').config(); | ||
|
||
// Babel hook | ||
require('babel-core/register'); | ||
|
||
// Server start | ||
// Server | ||
require('./src'); |
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,7 @@ | ||
{ | ||
"mongo": { | ||
"uri": "docker:37117/authserver", | ||
"user": "test", | ||
"pass": "password" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,20 @@ | ||
export default { | ||
mongo: { | ||
uri: 'localhost:32768', | ||
user: process.env.MONGO_USER || '', | ||
pass: process.env.MONGO_PASS || '' | ||
} | ||
} | ||
/** | ||
* This will read the file (expects JSON) defined at the path set in the CONFIG_PATH environment variable | ||
* CONFIG_PATH will be configured differently per envrionment | ||
* | ||
* Locally, this is set in the .env file via dotenv: https://github.com/motdotla/dotenv | ||
*/ | ||
import fs from 'fs'; | ||
import logger from './logger'; | ||
|
||
const pathToProperties = process.env.CONFIG_PATH; | ||
|
||
let config = {}; | ||
try { | ||
config = JSON.parse(fs.readFileSync(pathToProperties, 'utf8')); | ||
} catch(err) { | ||
logger.error(`Could not parse file as JSON at path ${pathToProperties}. This means our app will fail! Killing the app...`, err); | ||
process.exit(); | ||
} | ||
|
||
export default config; |
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,73 @@ | ||
/** | ||
* DB loading script that will load a client instance for client_credentials grants | ||
* DB connection configuration must be valid in local-properties.json if running locally | ||
* | ||
* This creates a {@link Client} with a random 8 byte Client ID and random 16 byte Client secret | ||
* associated with a {@link User} also with a random 8 byte username and 16 byte password | ||
* | ||
* Example: | ||
* - npm run db:create:client <client-name> | ||
* REQUIRED: `client-name` - A canonical name associated with client | ||
*/ | ||
import connect from '../../db'; | ||
import logger from '../../logger'; | ||
import crypto from 'crypto'; | ||
import { User, Client } from '../../models'; | ||
import mongoose from 'mongoose'; | ||
const connection = mongoose.connection; | ||
|
||
// The first two elements of process.argv are 'node' and the js file path being executed respectively | ||
const [clientName] = process.argv.slice(2); | ||
|
||
if (!clientName) { | ||
logger.error("A client name must be supplied to this command as the first argument! Example: `npm run db:create:client ADD`"); | ||
process.exit(); | ||
} | ||
|
||
connect(); | ||
connection.once('open', () => { | ||
loadClient(clientName) | ||
.then(() => { | ||
logger.info("Successfully loaded new Client"); | ||
process.exit(); | ||
}) | ||
.catch(err => { | ||
logger.error("Error loading new Client: ", err); | ||
process.exit(); | ||
}); | ||
}); | ||
connection.on('error', (err) => { | ||
logger.error('Error in Mongo connection:', err); | ||
process.exit(); | ||
}); | ||
|
||
async function loadClient(clientName) { | ||
const clientCredentials = { | ||
name: clientName, | ||
clientId: crypto.randomBytes(8).toString('hex'), | ||
clientSecret: crypto.randomBytes(16).toString('hex') | ||
}; | ||
|
||
const userCredentials = { | ||
username: crypto.randomBytes(8).toString('hex'), | ||
password: crypto.randomBytes(16).toString('hex') | ||
}; | ||
|
||
const newUser = await User.create({ | ||
username: userCredentials.username, | ||
password: userCredentials.password, | ||
// TODO: update script to take in scopes as an arg once scopes are implemented | ||
scope: null | ||
}); | ||
logger.info('Finished loading new User', newUser); | ||
|
||
const newClient = await | ||
Client.create({ | ||
"name": clientCredentials.name, | ||
"clientId": clientCredentials.clientId, | ||
"clientSecret": clientCredentials.clientSecret, | ||
"grants": ["client_credentials"], | ||
"user": newUser._id | ||
}); | ||
logger.info('Finished loading new client', newClient); | ||
} |
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,7 @@ | ||
require('babel-core/register'); | ||
|
||
// dotenv is only necessary when running this script locally | ||
require('dotenv').config(); | ||
|
||
// Runs create-client.js script | ||
require('./create-client.js'); |
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,19 @@ | ||
import mongoose from 'mongoose'; | ||
const Schema = mongoose.Schema; | ||
|
||
/** | ||
* An OAuth2 Client | ||
*/ | ||
export const Client = mongoose.model('Client', new Schema({ | ||
name: String, | ||
clientId: String, | ||
clientSecret: String, | ||
grants: [String], | ||
|
||
/** | ||
* If this Client supports client_credentials grant type, | ||
* this will hold the Client's User instance. | ||
* Otherwise, this will be undefined | ||
*/ | ||
user: {type: Schema.Types.ObjectId, ref: 'User'} | ||
})); |
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,21 @@ | ||
import mongoose from 'mongoose'; | ||
const Schema = mongoose.Schema; | ||
|
||
/** | ||
* An OAuth2 Token | ||
*/ | ||
export const Token = mongoose.model('Token', new Schema({ | ||
accessToken: String, | ||
accessTokenExpiresAt: Date, | ||
scope: [String], | ||
|
||
/** | ||
* The Client associated with the Token | ||
*/ | ||
client: {type: Schema.Types.ObjectId, ref: 'Client'}, | ||
|
||
/** | ||
* The User associated with the Token | ||
*/ | ||
user: {type: Schema.Types.ObjectId, ref: 'User'} | ||
})); |
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,11 @@ | ||
import mongoose from 'mongoose'; | ||
const Schema = mongoose.Schema; | ||
|
||
/** | ||
* An OAuth2 User | ||
*/ | ||
export const User = mongoose.model('User', new Schema({ | ||
username: String, | ||
password: String, | ||
scope: [String] | ||
})); |
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 @@ | ||
export * from './Client'; | ||
export * from './Token'; | ||
export * from './User'; |
Oops, something went wrong.