|
| 1 | +"use strict"; |
| 2 | +/// <reference path="./dtos/test.interfaces.d.ts" /> |
| 3 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 4 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 5 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 6 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 7 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 8 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 9 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 10 | + }); |
| 11 | +}; |
| 12 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 13 | +const test_dtos_1 = require("./dtos/test.dtos"); |
| 14 | +const chai = require("chai"); |
| 15 | +const { expect, assert } = chai; |
| 16 | +const index_1 = require("../src/index"); |
| 17 | +const TEST_URL = "https://test.servicestack.net"; |
| 18 | +//const TEST_URL = "http://localhost:56500"; |
| 19 | +const createJwt = (opt = {}) => { |
| 20 | + const request = Object.assign(new test_dtos_1.CreateJwt(), opt); |
| 21 | + if (!request.userAuthId) |
| 22 | + request.userAuthId = "1"; |
| 23 | + if (!request.displayName) |
| 24 | + request.displayName = "test jwt"; |
| 25 | + if (!request.email) |
| 26 | + request.email = "[email protected]"; |
| 27 | + return request; |
| 28 | +}; |
| 29 | +//Clear existing User Session |
| 30 | +const clearSession = (client) => __awaiter(void 0, void 0, void 0, function* () { |
| 31 | + let logout = new test_dtos_1.Authenticate(); |
| 32 | + logout.provider = "logout"; |
| 33 | + yield client.post(logout); |
| 34 | +}); |
| 35 | +const supportsServerEvents = () => typeof global.EventSource != "undefined"; |
| 36 | +describe('JsonServiceClient Auth Tests', () => { |
| 37 | + let client; |
| 38 | + beforeEach(() => { |
| 39 | + client = new index_1.JsonServiceClient(TEST_URL); |
| 40 | + }); |
| 41 | + it("Can auth with JWT", () => __awaiter(void 0, void 0, void 0, function* () { |
| 42 | + const request = createJwt(); |
| 43 | + let response = yield client.post(request); |
| 44 | + client.bearerToken = response.token; |
| 45 | + let testAuth = yield client.get(new test_dtos_1.TestAuth()); |
| 46 | + expect(testAuth.userId).eq("1"); |
| 47 | + expect(testAuth.displayName).eq("test jwt"); |
| 48 | + expect(testAuth.sessionId).not.empty; |
| 49 | + })); |
| 50 | + it("Does fire onAuthenticationRequired callback on 401", () => __awaiter(void 0, void 0, void 0, function* () { |
| 51 | + let count = 0; |
| 52 | + client.onAuthenticationRequired = () => { |
| 53 | + count++; |
| 54 | + return Promise.resolve(null); |
| 55 | + }; |
| 56 | + try { |
| 57 | + yield client.get(new test_dtos_1.TestAuth()); |
| 58 | + assert.fail("should throw"); |
| 59 | + } |
| 60 | + catch (e) { |
| 61 | + let status = e.responseStatus; |
| 62 | + expect(status.errorCode).eq("401"); |
| 63 | + expect(status.message).eq("Unauthorized"); |
| 64 | + expect(count).eq(1); |
| 65 | + } |
| 66 | + })); |
| 67 | + it("Can use onAuthenticationRequired to auth client", () => __awaiter(void 0, void 0, void 0, function* () { |
| 68 | + let count = 0; |
| 69 | + client.onAuthenticationRequired = () => { |
| 70 | + count++; |
| 71 | + client.userName = "test"; |
| 72 | + client.password = "test"; |
| 73 | + return Promise.resolve(null); |
| 74 | + }; |
| 75 | + let response = yield client.get(new test_dtos_1.TestAuth()); |
| 76 | + expect(count).eq(1); |
| 77 | + })); |
| 78 | + it("Can use onAuthenticationRequired to fetch new token", () => __awaiter(void 0, void 0, void 0, function* () { |
| 79 | + let count = 0; |
| 80 | + client.onAuthenticationRequired = () => __awaiter(void 0, void 0, void 0, function* () { |
| 81 | + count++; |
| 82 | + let authClient = new index_1.JsonServiceClient(TEST_URL); |
| 83 | + authClient.userName = "test"; |
| 84 | + authClient.password = "test"; |
| 85 | + const response = yield authClient.get(new test_dtos_1.Authenticate()); |
| 86 | + client.bearerToken = authClient.cookies['ss-tok'].value; |
| 87 | + }); |
| 88 | + let response = yield client.get(new test_dtos_1.TestAuth()); |
| 89 | + expect(count).eq(1); |
| 90 | + })); |
| 91 | + it("Can use onAuthenticationRequired to fetch new token after expired token", () => __awaiter(void 0, void 0, void 0, function* () { |
| 92 | + let count = 0; |
| 93 | + client.onAuthenticationRequired = () => __awaiter(void 0, void 0, void 0, function* () { |
| 94 | + count++; |
| 95 | + let createFreshJwt = createJwt(); |
| 96 | + const freshJwt = yield client.post(createFreshJwt); |
| 97 | + client.bearerToken = freshJwt.token; |
| 98 | + }); |
| 99 | + let createExpiredJwt = createJwt(); |
| 100 | + createExpiredJwt.jwtExpiry = "2000-01-01"; |
| 101 | + const expiredJwt = yield client.post(createExpiredJwt); |
| 102 | + client.bearerToken = expiredJwt.token; |
| 103 | + let response = yield client.get(new test_dtos_1.TestAuth()); |
| 104 | + expect(count).eq(1); |
| 105 | + })); |
| 106 | + it("Can reauthenticate after an auto refresh access token", () => __awaiter(void 0, void 0, void 0, function* () { |
| 107 | + let client = new index_1.JsonServiceClient(TEST_URL); |
| 108 | + let auth = new test_dtos_1.Authenticate(); |
| 109 | + auth.provider = "credentials"; |
| 110 | + auth.userName = "test"; |
| 111 | + auth.password = "test"; |
| 112 | + let authResponse = yield client.post(auth); |
| 113 | + let refreshToken = authResponse.refreshToken; |
| 114 | + let createExpiredJwt = createJwt(); |
| 115 | + createExpiredJwt.jwtExpiry = "2000-01-01"; |
| 116 | + const expiredJwt = yield client.post(createExpiredJwt); |
| 117 | + let bearerToken = expiredJwt.token; |
| 118 | + yield clearSession(client); |
| 119 | + client = new index_1.JsonServiceClient(TEST_URL); |
| 120 | + client.bearerToken = bearerToken; |
| 121 | + client.refreshToken = refreshToken; |
| 122 | + auth.password = "notvalid"; |
| 123 | + try { |
| 124 | + yield client.post(auth); |
| 125 | + assert.fail("should throw"); |
| 126 | + } |
| 127 | + catch (e) { |
| 128 | + let status = e.responseStatus; |
| 129 | + expect(status.errorCode).eq("Unauthorized"); |
| 130 | + expect(status.message).eq("Invalid Username or Password"); |
| 131 | + } |
| 132 | + })); |
| 133 | + it('Does fetch AccessToken using RefreshTokenCookies', () => __awaiter(void 0, void 0, void 0, function* () { |
| 134 | + let client = new index_1.JsonServiceClient(TEST_URL); |
| 135 | + let authResponse = yield client.post(new test_dtos_1.Authenticate({ |
| 136 | + provider: "credentials", |
| 137 | + userName: "test", |
| 138 | + password: "test" |
| 139 | + })); |
| 140 | + expect(client.useTokenCookie).eq(true); |
| 141 | + let request = new test_dtos_1.Secured({ name: "test" }); |
| 142 | + let response = yield client.post(request); |
| 143 | + expect(response.result).eq(request.name); |
| 144 | + yield client.post(new test_dtos_1.InvalidateLastAccessToken()); |
| 145 | + response = yield client.post(request); |
| 146 | + expect(response.result).eq(request.name); |
| 147 | + })); |
| 148 | + it("Invalid RefreshToken throws RefreshTokenException ErrorResponse", () => __awaiter(void 0, void 0, void 0, function* () { |
| 149 | + let client = new index_1.JsonServiceClient(TEST_URL); |
| 150 | + client.refreshToken = "Invalid.Refresh.Token"; |
| 151 | + try { |
| 152 | + let response = yield client.get(new test_dtos_1.TestAuth()); |
| 153 | + assert.fail("should throw"); |
| 154 | + } |
| 155 | + catch (e) { |
| 156 | + expect(e.type).eq("RefreshTokenException"); |
| 157 | + expect(e.responseStatus.errorCode).eq("ArgumentException"); |
| 158 | + expect(e.responseStatus.message).eq("Illegal base64url string!"); |
| 159 | + } |
| 160 | + })); |
| 161 | + it("Expires RefreshToken throws RefreshTokenException", () => __awaiter(void 0, void 0, void 0, function* () { |
| 162 | + let client = new index_1.JsonServiceClient(TEST_URL); |
| 163 | + let createExpiredJwt = new test_dtos_1.CreateRefreshJwt(); |
| 164 | + createExpiredJwt.jwtExpiry = "2000-01-01"; |
| 165 | + const expiredJwt = yield client.post(createExpiredJwt); |
| 166 | + client.refreshToken = expiredJwt.token; |
| 167 | + try { |
| 168 | + let response = yield client.get(new test_dtos_1.TestAuth()); |
| 169 | + assert.fail("should throw"); |
| 170 | + } |
| 171 | + catch (e) { |
| 172 | + expect(e.type).eq("RefreshTokenException"); |
| 173 | + expect(e.responseStatus.errorCode).eq("TokenException"); |
| 174 | + expect(e.responseStatus.message).eq("Token has expired"); |
| 175 | + } |
| 176 | + })); |
| 177 | + it("Can authenticate with ServerEvents using JWT", done => { |
| 178 | + (() => __awaiter(void 0, void 0, void 0, function* () { |
| 179 | + if (!supportsServerEvents()) |
| 180 | + return done(); |
| 181 | + let authClient = new index_1.JsonServiceClient(TEST_URL); |
| 182 | + const request = createJwt(); |
| 183 | + let response = yield authClient.post(request); |
| 184 | + // Alternatives for browsers is to convert the JWT to a cookie so it's sent in future HTTP requests |
| 185 | + // a) Typed API |
| 186 | + // let client = new JsonServiceClient(TEST_URL); |
| 187 | + // client.setBearerToken(response.token); |
| 188 | + // await client.post(new dtos.ConvertSessionToToken()); |
| 189 | + // b) Using fetch directly without types |
| 190 | + // let headers = new Headers(); |
| 191 | + // headers.set("Authorization", "Bearer " + response.token); |
| 192 | + // await fetch(TEST_URL + "/session-to-token", |
| 193 | + // { method:"POST", headers, credentials:"include" }); |
| 194 | + // Remote Server needs `new JwtAuthProvider { AllowInQueryString = true }` |
| 195 | + let sseClient = new index_1.ServerEventsClient(TEST_URL, ["*"], { |
| 196 | + // Works in both browers + node.exe server apps |
| 197 | + resolveStreamUrl: url => (0, index_1.appendQueryString)(url, { "ss-tok": response.token }), |
| 198 | + handlers: { |
| 199 | + onConnect: (e => { |
| 200 | + sseClient.stop(); |
| 201 | + done(); |
| 202 | + }) |
| 203 | + }, |
| 204 | + onException: (e) => { |
| 205 | + expect(JSON.stringify(e)).null; |
| 206 | + }, |
| 207 | + }); |
| 208 | + // c) also works in browsers: |
| 209 | + // sseClient.serviceClient.setBearerToken(response.token); |
| 210 | + // await sseClient.serviceClient.post(new dtos.ConvertSessionToToken()); |
| 211 | + sseClient.start(); |
| 212 | + }))(); |
| 213 | + }); |
| 214 | +}); |
0 commit comments