@aytacworld/express-module-oauth-server
v4.0.0
Published
Adding oauth server capability for @aytacworld/express
Downloads
6
Readme
@aytacworld/express-module-oauth-server
This module is ment to be used with @aytacworld/express.
It will add oauth server capabilities.
Needed modules
For this module you need 1.authDb module to be loaded before loading this module.
The needed modules needs to be loaded before loading oauth module.
There are already 2 modules for authDb module.
- @aytacworld/express-module-memory uses in-memory database
- @aytacworld/express-module-mongo uses mongodb
Or you can create it yourself
Installation
using npm
npm i @aytacworld/express-module-oauth-server
using yarn
yarn add @aytacworld/express-module-oauth-server
Usage
app.js
const Express = require('@aytacworld/express');
const ExpressMongo = require('@aytacworld/express-module-mongo');
const ExpressOauthServer = require('@aytacworld/express-module-oauth-server');
const app = new Express({});
app.module(ExpressMongo, ExpressOauthServer);
app.listen();
Config
{
"EXPRESS_MODULE_OAUTH_SERVER_DISABLE_VIEW": true, // Boolean, default: undefined, this will don't add default pug files in your project, so you need to create your own login page
"EXPRESS_MODULE_OAUTH_SERVER_DECISION_VIEW": "my-custom-login-view", // String, default: 'aytacworld-express-module-oauth-server-decision'
"EXPRESS_MODULE_OAUTH_SERVER_CSS": "/public/oauth.css", // String, default: undefined
"EXPRESS_MODULE_OAUTH_SERVER_TOKEN_LENGTH": 345, // Number, default: 256
"EXPRESS_MODULE_OAUTH_SERVER_EXPIRE_DEFAULT": 3600, // Number, default: 1800
}
Updating decision dialog page
dialog.pug
p.module-oauth-server-p.module-oauth-server-p-username #{username}
p.module-oauth-server-p.module-oauth-server-p-client #{client} is requesting access to your account
p.module-oauth-server-p.module-oauth-server-p-question Do you approve?
form(action='/oauth/decision' method='POST').module-oauth-server-form
input(name='transaction_id' type='hidden' value=`${transactionId}`)
input(type='submit' value="Allow" id="allow").module-oauth-server-input.module-oauth-server-input-allow
input(type='submit' value="Deny" id="deny" name='cancel').module-oauth-server-input.module-oauth-server-input-deny
Database structure
{
users: {
static comparePassword(username, password): Promise<user|false>;
},
clients: {
static findById(id): Promise<client>;
static findByClientId(clientId): Promise<client>;
static compareSecret(username, secret): Promise<client|false>;
},
tokens: {
static find(key): Promise<token>;
static findByUserIdAndClientId(userId, clientId): Promise<token>;
static findByTransactionId(transactionId): Promise<token>;
static saveCode(authCode, userId, clientId, redirectUri, state, transactionId): Promise<void>;
static exchangeAuthCodeForAccessToken(authCode, accessToken, refreshToken, expiresIn): Promise<void>;
static refreshAccessToken(refreshToken, accessToken, expiresIn): Promise<void>;
}
}
User model
class User {
id: any;
username: string;
password: string;
}
INFO: don't forget to hash the password before saving in database!
Client model
class Client {
name: string;
clientId: string;
clientSecret: string;
redirectUrl: string;
}
Token model
class Token {
token: string;
userId: string;
clientId: string;
redirectUri: string;
refreshToken: string;
expiresIn: number;
authCode: string;
state: string;
transactionId: string;
codeChallenge: string;
}
MIT License
Copyright (c) 2019 Adem Aytaç
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.