all files / dist/ index.js

16.9% Statements 24/142
15.87% Branches 10/63
15.38% Functions 6/39
18.05% Lines 24/133
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 233 234 235 236                                                                                                                                                                                                                                                                                                                                                                                                                                       
"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");
const oauth2_error_1 = require("./models/oauth2-error");
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;
var oauth2_error_2 = require("./models/oauth2-error");
exports.OAuth2FrameworkError = oauth2_error_2.OAuth2FrameworkError;
class OAuth2Framework {
    constructor(model, secret) {
        this.model = model;
        this.secret = secret;
    }
    accessTokenRequest(grant_type, code, redirect_uri, client_id, client_secret, username, password, scopes, request) {
        return __awaiter(this, void 0, void 0, function* () {
            this.throwIfInvalidGrantType(grant_type);
            const client = yield this.findClientAndValidate(client_id, redirect_uri, scopes, request);
            if (grant_type === 'password') {
                const validCredentials = yield this.model.validateCredentials(client_id, username, password, request);
                this.throwIfInvalidCredentials(validCredentials);
                return this.model.generateAccessToken(client_id, username, scopes, request);
            }
            else if (grant_type === 'authorization_code') {
                this.throwIfClientDoesNotMatchClientSecret(client, client_secret);
                const token = yield this.model.validateCode(code, request);
                return this.model.generateAccessToken(token.client_id, token.username, token.scopes, request);
            }
        });
    }
    authorizationRequest(response_type, client_id, redirect_uri, scopes, state, username, password, request) {
        return __awaiter(this, void 0, void 0, function* () {
            this.throwIfInvalidResponseType(response_type);
            const client = yield this.findClientAndValidate(client_id, redirect_uri, scopes, request);
            const validCredentials = yield this.model.validateCredentials(client_id, username, password, request);
            this.throwIfInvalidCredentials(validCredentials);
            switch (response_type) {
                case 'code':
                    return this.model.generateCode(client_id, username, scopes, request);
                case 'token':
                    return this.model.generateAccessToken(client_id, username, scopes, request);
            }
        });
    }
    validateAccessToken(access_token, request) {
        return __awaiter(this, void 0, void 0, function* () {
            const token = yield this.model.validateAccessToken(access_token, request);
            if (!token) {
                return false;
            }
            return true;
        });
    }
    decodeAccessToken(access_token, request) {
        return __awaiter(this, void 0, void 0, function* () {
            const token = yield this.model.validateAccessToken(access_token, request);
            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);
            this.throwIfClientNull(client);
            if (!client.allowForgotPassword) {
                throw new oauth2_error_1.OAuth2FrameworkError('forgot_password_not_enabled', 'The forgot password functionality is not enabled for this 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 oauth2_error_1.OAuth2FrameworkError('invalid_token', 'Invalid token');
            }
            const client = yield this.model.findClient(decodedToken.client_id, request);
            this.throwIfClientNull(client);
            if (!client.allowRegister) {
                throw new oauth2_error_1.OAuth2FrameworkError('register_not_enabled', 'The register functionality is not enabled for this 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);
            this.throwIfClientNull(client);
            if (!client.allowRegister) {
                throw new oauth2_error_1.OAuth2FrameworkError('register_not_enabled', 'The register functionality is not enabled for this 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 oauth2_error_1.OAuth2FrameworkError('invalid_token', 'Invalid token');
            }
            const client = yield this.model.findClient(decodedToken.client_id, request);
            this.throwIfClientNull(client);
            if (!client.allowForgotPassword) {
                throw new oauth2_error_1.OAuth2FrameworkError('forgot_password_not_enabled', 'The forgot password functionality is not enabled for this 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;
        });
    }
    decodeJWT(jwt) {
        return new Promise((resolve, reject) => {
            jsonwebtoken.verify(jwt, this.secret, (err, decodedCode) => {
                if (err) {
                    resolve(null);
                    return;
                }
                resolve(decodedCode);
            });
        });
    }
    findClientAndValidate(client_id, redirect_uri, scopes, request) {
        return __awaiter(this, void 0, void 0, function* () {
            const client = yield this.model.findClient(client_id, request);
            this.throwIfClientNull(client);
            this.throwIfClientDoesNotContainUri(client, redirect_uri);
            this.throwIfClientDoesNotContainScope(client, scopes);
            return client;
        });
    }
    generateEmailVerificationToken(client_id, username, return_url) {
        return jsonwebtoken.sign({
            client_id,
            return_url,
            type: 'email-verification',
            username,
        }, this.secret, {
            expiresIn: '60m',
        });
    }
    generateResetPasswordToken(client_id, username, return_url) {
        return jsonwebtoken.sign({
            client_id,
            return_url,
            type: 'reset-password',
            username,
        }, this.secret, {
            expiresIn: '60m',
        });
    }
    throwIfClientDoesNotContainScope(client, scopes) {
        if (scopes.length !== 0 && scopes.filter((x) => client.allowedScopes.indexOf(x) === -1).length !== 0) {
            throw new oauth2_error_1.OAuth2FrameworkError('invalid_scopes', 'Invalid scopes');
        }
    }
    throwIfClientDoesNotContainUri(client, uri) {
        if (client.redirectUris.indexOf(uri) === -1) {
            throw new oauth2_error_1.OAuth2FrameworkError('invalid_redirect_uri', 'Invalid redirect uri');
        }
    }
    throwIfClientDoesNotMatchClientSecret(client, client_secret) {
        if (client.secret !== client_secret) {
            throw new oauth2_error_1.OAuth2FrameworkError('invalid_secret', 'Invalid client_secret');
        }
    }
    throwIfClientNull(client) {
        if (!client) {
            throw new oauth2_error_1.OAuth2FrameworkError('invalid_client_id', 'Invalid client id');
        }
    }
    throwIfInvalidCredentials(validCredentials) {
        if (!validCredentials) {
            throw new oauth2_error_1.OAuth2FrameworkError('invalid_credentials', 'Invalid credentials');
        }
    }
    throwIfInvalidGrantType(grant_type) {
        Eif (grant_type !== 'authorization_code' && grant_type !== 'password') {
            throw new oauth2_error_1.OAuth2FrameworkError('invalid_grant_type', 'Invalid grant type');
        }
    }
    throwIfInvalidResponseType(response_type) {
        if (response_type !== 'code' && response_type !== 'token') {
            throw new oauth2_error_1.OAuth2FrameworkError('invalid_response_type', 'Invalid response type');
        }
    }
}
exports.OAuth2Framework = OAuth2Framework;
//# sourceMappingURL=index.js.map