all files / adal-ts2/src/ aad.url.builder.ts

93.18% Statements 41/44
77.78% Branches 14/18
100% Functions 6/6
93.18% Lines 41/44
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  1× 1×   12×     4×   12× 12× 12× 12× 12× 12×   1× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4×   1× 4× 4× 4× 4×   1× 4× 4× 4× 4×     4× 4× 4×     4×       4× 4×     1×   1× 1×                                                  
import { GuidGenerator } from './guid.generator';
import { AadUrlConfig } from './aad.url.config';
export class AadUrlBuilder {
 
    private nonce: string;
    private tenant: string;
    private responseType: string;
    private clientId: string;
    private resource: string;
    private redirectUri: string;
    private state: string;
    private slice: string;
    private clientRequestId: string;
    private libVersion: string;
    private extraQueryParameter: string;
    private guidGenerator: GuidGenerator;
    public static MicrosoftLoginUrl: string = 'https://login.microsoftonline.com/';
 
    constructor(guidGenerator: GuidGenerator) {
        this.guidGenerator = guidGenerator;
 
        this.state = this.guidGenerator.generate();
        this.clientRequestId = this.guidGenerator.generate();
        this.responseType = 'id_token';
        this.libVersion = '1.0.0';
        this.redirectUri = window.location.href;
    }
 
    public with(options: AadUrlConfig): AadUrlBuilder {
 
        this.nonce = options.nonce;
        this.tenant = options.tenant;
        this.clientId = options.clientId;
        this.responseType = options.responseType || this.responseType;
        this.redirectUri = options.redirectUri || this.redirectUri;
        this.state = options.state;
        this.slice = options.slice || this.slice;
        this.clientRequestId = options.clientRequestId || this.clientRequestId;
        this.libVersion = options.libVersion || this.libVersion;
        Ithis.extraQueryParameter = options.extraQueryParameter || this.extraQueryParameter;
        return this;
    }
 
    public build() {
I
        var urlNavigate = AadUrlBuilder.MicrosoftLoginUrl + this.tenant + '/oauth2/authorize';
        urlNavigate = urlNavigate + this.serialize() + this.addLibMetadata();
        IurlNavigate = urlNavigate + '&nonce=' + encodeURIComponent(this.nonce);
        return urlNavigate;
    }
 
    private serialize(): string {
 
        var str: any = [];
        str.push('?response_type=' + this.responseType);
        str.push('client_id=' + encodeURIComponent(this.clientId));
        if (this.resource) {
            str.push('resource=' + encodeURIComponent(this.resource));
        }
 
        str.push('redirect_uri=' + encodeURIComponent(this.redirectUri));
        str.push('state=' + encodeURIComponent(this.state));
 
        if (this.slice) {
            str.push('slice=' + encodeURIComponent(this.slice));
        }
 
        if (this.extraQueryParameter) {
            str.push(this.extraQueryParameter);
        }
 
        // var correlationId = this.clientRequestId ? obj.correlationId : new GuidGenerator().generate();
        str.push('client-request-id=' + encodeURIComponent(this.clientRequestId));
 
        return str.join('&');
    };
 
    private addLibMetadata = function () {
        // x-client-SKU
        // x-client-Ver
        return '&x-client-SKU=Js&x-client-Ver=' + this.libVersion;
    };
 
}