1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | 1× 26× 41× 1× 55× 26× 1× 1× 1× 1× 1× 1× 1× 1× 22× 22× 9× 9× 9× 8× 1× 7× 1× 6× 1× 5× 5× 1× 5× 3× 2× 1× 1× 11× 11× 11× 10× 1× 9× 1× 9× 4× 4× 1× 3× 5× 4× 4× 2× 2× 1× 1× 1× 2× 2× 2× 1× 1× 4× 4× 4× 1× 3× 1× 2× 2× 2× 2× 2× 2× 1× | "use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const jsonwebtoken = require("jsonwebtoken"); var client_1 = require("./models/client"); exports.Client = client_1.Client; var token_1 = require("./models/token"); exports.Token = token_1.Token; var router_1 = require("./router"); exports.OAuth2FrameworkRouter = router_1.OAuth2FrameworkRouter; class OAuth2Framework { constructor(model, secret) { this.model = model; this.secret = secret; } authorizationRequest(response_type, client_id, redirect_uri, scopes, state, username, password, request) { return __awaiter(this, void 0, void 0, function* () { const client = yield this.model.findClient(client_id, request); if (response_type === 'code' || response_type === 'token') { if (!client) { throw new Error('Invalid client_id'); } if (client.redirectUris.indexOf(redirect_uri) === -1) { throw new Error('Invalid redirect_uri'); } if (scopes.length !== 0 && scopes.filter((x) => client.allowedScopes.indexOf(x) === -1).length !== 0) { throw new Error('Invalid scopes'); } const validCredentials = yield this.model.validateCredentials(client_id, username, password, request); if (!validCredentials) { return null; } } if (response_type === 'code') { return this.model.generateCode(client_id, username, scopes); } else if (response_type === 'token') { return this.model.generateAccessToken(client_id, username, scopes); } else { throw new Error('Invalid response_type'); } }); } accessTokenRequest(grant_type, code, redirect_uri, client_id, client_secret, username, password, scopes, request) { return __awaiter(this, void 0, void 0, function* () { const client = yield this.model.findClient(client_id, request); if (grant_type === 'authorization_code' || grant_type === 'password') { if (!client) { throw new Error('Invalid client_id'); } if (client.redirectUris.indexOf(redirect_uri) === -1) { throw new Error('Invalid redirect_uri'); } } if (grant_type === 'password') { const validCredentials = yield this.model.validateCredentials(client_id, username, password, request); if (!validCredentials) { return null; } return this.model.generateAccessToken(client_id, username, scopes); } else if (grant_type === 'authorization_code') { const token = yield this.model.validateCode(code); if (!token) { throw new Error('Invalid code'); } if (client.secret !== client_secret) { throw new Error('Invalid client_secret'); } return this.model.generateAccessToken(token.client_id, token.username, token.scopes); } else { throw new Error('Invalid grant_type'); } }); } validateAccessToken(access_token) { return __awaiter(this, void 0, void 0, function* () { const token = yield this.model.validateAccessToken(access_token); if (!token) { return false; } return true; }); } decodeAccessToken(access_token) { return __awaiter(this, void 0, void 0, function* () { const token = yield this.model.validateAccessToken(access_token); if (!token) { return null; } return token; }); } forgotPasswordRequest(client_id, username, response_type, redirect_uri, state, request) { return __awaiter(this, void 0, void 0, function* () { const client = yield this.model.findClient(client_id, request); if (!client) { throw new Error('Invalid client_id'); } if (!client.allowForgotPassword) { throw new Error('Function not enabled for client'); } const returnUrl = `authorize?response_type=${response_type}&client_id=${client_id}&redirect_uri=${redirect_uri}&state=${state}`; const resetPasswordToken = this.generateResetPasswordToken(client_id, username, returnUrl); const resetPasswordUrl = `/reset-password?token=${resetPasswordToken}`; const result = yield this.model.sendForgotPasswordEmail(client_id, username, resetPasswordUrl, request); return result; }); } emailVerificationRequest(token, request) { return __awaiter(this, void 0, void 0, function* () { const decodedToken = yield this.decodeEmailVerificationToken(token); if (!decodedToken) { throw new Error('Invalid token'); } const client = yield this.model.findClient(decodedToken.client_id, request); if (!client) { throw new Error('Invalid client_id'); } if (!client) { throw new Error('Invalid client_id'); } if (!client.allowRegister) { throw new Error('Function not enabled for client'); } const result = yield this.model.verify(decodedToken.client_id, decodedToken.username, request); return result; }); } registerRequest(client_id, emailAddress, username, password, response_type, redirect_uri, state, request) { return __awaiter(this, void 0, void 0, function* () { const client = yield this.model.findClient(client_id, request); if (!client) { throw new Error('Invalid client_id'); } if (!client.allowRegister) { throw new Error('Function not enabled for client'); } const returnUrl = `authorize?response_type=${response_type}&client_id=${client_id}&redirect_uri=${redirect_uri}&state=${state}`; const emailVerificationToken = this.generateEmailVerificationToken(client_id, username, returnUrl); const emailVerificationUrl = `/email-verification?token=${emailVerificationToken}`; const result = yield this.model.register(client_id, emailAddress, username, password, request); if (result) { const emailResult = yield this.model.sendVerificationEmail(client_id, emailAddress, username, emailVerificationUrl, request); } return result; }); } resetPasswordRequest(token, password, request) { return __awaiter(this, void 0, void 0, function* () { const decodedToken = yield this.decodeResetPasswordToken(token); if (!decodedToken) { throw new Error('Invalid token'); } const client = yield this.model.findClient(decodedToken.client_id, request); if (!client) { throw new Error('Invalid client_id'); } if (!client.allowForgotPassword) { throw new Error('Function not enabled for client'); } const result = yield this.model.resetPassword(decodedToken.client_id, decodedToken.username, password, request); return result; }); } decodeResetPasswordToken(token) { return __awaiter(this, void 0, void 0, function* () { const decodedToken = yield this.decodeJWT(token); if (!decodedToken) { return null; } if (decodedToken.type !== 'reset-password') { return null; } return decodedToken; }); } decodeEmailVerificationToken(token) { return __awaiter(this, void 0, void 0, function* () { const decodedToken = yield this.decodeJWT(token); if (!decodedToken) { return null; } if (decodedToken.type !== 'email-verification') { return null; } return decodedToken; }); } generateResetPasswordToken(client_id, username, return_url) { return jsonwebtoken.sign({ client_id, return_url, type: 'reset-password', username, }, this.secret, { expiresIn: '60m', }); } generateEmailVerificationToken(client_id, username, return_url) { return jsonwebtoken.sign({ client_id, return_url, type: 'email-verification', username, }, this.secret, { expiresIn: '60m', }); } decodeJWT(jwt) { return new Promise((resolve, reject) => { jsonwebtoken.verify(jwt, this.secret, (err, decodedCode) => { if (err) { resolve(null); return; } resolve(decodedCode); }); }); } } exports.OAuth2Framework = OAuth2Framework; //# sourceMappingURL=index.js.map |