"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 |