All files / src/Authentication JwtService.js

0% Statements 0/59
0% Branches 0/16
0% Functions 0/16
0% Lines 0/58
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                                                                                                                                                                                                                         
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("@reactivex/rxjs");
const SN_1 = require("../SN");
const _1 = require("./");
class JwtService {
    constructor(_httpProviderRef, _repositoryUrl, _tokenTemplate, Persist) {
        this._httpProviderRef = _httpProviderRef;
        this._repositoryUrl = _repositoryUrl;
        this._tokenTemplate = _tokenTemplate;
        this.Persist = Persist;
        this._visitorName = 'BuiltIn\\Visitor';
        this._stateSubject = new rxjs_1.BehaviorSubject(_1.LoginState.Pending);
        this._tokenStore = new _1.TokenStore(this._repositoryUrl, this._tokenTemplate, (this.Persist === 'session') ? _1.TokenPersist.Session : _1.TokenPersist.Expiration);
        this._stateSubject = new rxjs_1.BehaviorSubject(_1.LoginState.Pending);
        this.State.subscribe((s) => {
            if (this._tokenStore.AccessToken.IsValid()) {
                this._httpProviderRef.SetGlobalHeader('X-Access-Data', this._tokenStore.AccessToken.toString());
            }
            else {
                this._httpProviderRef.UnsetGlobalHeader('X-Access-Data');
            }
        });
        this.CheckForUpdate();
    }
    get CurrentUser() {
        if (this._tokenStore.AccessToken.IsValid() || this._tokenStore.RefreshToken.IsValid()) {
            return this._tokenStore.AccessToken.Username || this._tokenStore.RefreshToken.Username;
        }
        return this._visitorName;
    }
    get State() {
        return this._stateSubject.distinctUntilChanged();
    }
    get CurrentState() {
        return this._stateSubject.getValue();
    }
    CheckForUpdate() {
        if (this._tokenStore.AccessToken.IsValid()) {
            this._stateSubject.next(_1.LoginState.Authenticated);
            return rxjs_1.Observable.from([false]);
        }
        if (!this._tokenStore.RefreshToken.IsValid()) {
            this._stateSubject.next(_1.LoginState.Unauthenticated);
            return rxjs_1.Observable.from([false]);
        }
        this._stateSubject.next(_1.LoginState.Pending);
        return this.execTokenRefresh();
    }
    execTokenRefresh() {
        const refresh = this._httpProviderRef.Ajax(_1.RefreshResponse, {
            method: 'POST',
            url: SN_1.ODataHelper.joinPaths(this._repositoryUrl, 'sn-token/refresh'),
            headers: {
                'X-Refresh-Data': this._tokenStore.RefreshToken.toString(),
                'X-Authentication-Type': 'Token',
            },
        });
        refresh.subscribe((response) => {
            this._tokenStore.AccessToken = _1.Token.FromHeadAndPayload(response.access);
            this._stateSubject.next(_1.LoginState.Authenticated);
        }, (err) => {
            this._stateSubject.next(_1.LoginState.Unauthenticated);
        });
        return refresh.map((response) => true);
    }
    handleAuthenticationResponse(response) {
        this._tokenStore.AccessToken = _1.Token.FromHeadAndPayload(response.access);
        this._tokenStore.RefreshToken = _1.Token.FromHeadAndPayload(response.refresh);
        if (this._tokenStore.AccessToken.IsValid()) {
            this._stateSubject.next(_1.LoginState.Authenticated);
            return true;
        }
        this._stateSubject.next(_1.LoginState.Unauthenticated);
        return false;
    }
    Login(username, password) {
        const sub = new rxjs_1.Subject();
        this._stateSubject.next(_1.LoginState.Pending);
        const authToken = new Buffer(`${username}:${password}`).toString('base64');
        this._httpProviderRef.Ajax(_1.LoginResponse, {
            method: 'POST',
            url: SN_1.ODataHelper.joinPaths(this._repositoryUrl, 'sn-token/login'),
            headers: {
                'X-Authentication-Type': 'Token',
                'Authorization': `Basic ${authToken}`,
            },
        })
            .subscribe((r) => {
            const result = this.handleAuthenticationResponse(r);
            sub.next(result);
        }, (err) => {
            this._stateSubject.next(_1.LoginState.Unauthenticated);
            sub.next(false);
        });
        return sub.asObservable();
    }
    Logout() {
        this._tokenStore.AccessToken = _1.Token.CreateEmpty();
        this._tokenStore.RefreshToken = _1.Token.CreateEmpty();
        this._stateSubject.next(_1.LoginState.Unauthenticated);
        return this._httpProviderRef.Ajax(_1.LoginResponse, {
            method: 'POST',
            url: SN_1.ODataHelper.joinPaths(this._repositoryUrl, 'sn-token/logout'),
        }).map(() => true);
    }
}
exports.JwtService = JwtService;
//# sourceMappingURL=JwtService.js.map