<commit_before>import { User } from './user';

declare function escape(v: string): string;

export class UserDecoder {
    public decode(encoded: string): User {

        var jwtDecoded = this.decodeJwt(encoded);
        if (!jwtDecoded) {
            throw Error('Failed to decode value. Value has invalid format.');
        }

        var decodedPayLoad = this.safeDecodeBase64(jwtDecoded.JWSPayload);

        let user = JSON.parse(decodedPayLoad);

        //if (!user || !user.hasOwnProperty('aud')) throw new Error('');

        return <User>user;
    }

    private safeDecodeBase64(value: string) {

        var base64Decoded = this.base64DecodeStringUrlSafe(value);
        if (!base64Decoded) {
            //this.info('The returned id_token could not be base64 url safe decoded.');
            throw Error('Failed to base64 decode value. Value has invalid format.');
        }

        return base64Decoded;
    }

    private decodeJwt = function (jwtToken: string) {
        
        var idTokenPartsRegex = /^([^\.\s]*)\.([^\.\s]+)\.([^\.\s]*)$/;

        var matches = idTokenPartsRegex.exec(jwtToken);
        if (!matches || matches.length < 4) {
            throw new Error(`Failed to decode Jwt token. The token has in valid format. Actual token: '${jwtToken}'`);
        }

        var crackedToken = {
            header: matches[1],
            JWSPayload: matches[2],
            JWSSig: matches[3]
        };

        return crackedToken;
    };

    private base64DecodeStringUrlSafe = function (base64IdToken: string) {
        // html5 should support atob function for decoding
        base64IdToken = base64IdToken.replace(/-/g, '+').replace(/_/g, '/');<commit_msg>fix: minor typo in error message<commit_after>import { User } from './user';

declare function escape(v: string): string;

export class UserDecoder {
    public decode(encoded: string): User {

        var jwtDecoded = this.decodeJwt(encoded);
        if (!jwtDecoded) {
            throw Error('Failed to decode value. Value has invalid format.');
        }

        var decodedPayLoad = this.safeDecodeBase64(jwtDecoded.JWSPayload);

        let user = JSON.parse(decodedPayLoad);

        //if (!user || !user.hasOwnProperty('aud')) throw new Error('');

        return <User>user;
    }

    private safeDecodeBase64(value: string) {

        var base64Decoded = this.base64DecodeStringUrlSafe(value);
        if (!base64Decoded) {
            //this.info('The returned id_token could not be base64 url safe decoded.');
            throw Error('Failed to base64 decode value. Value has invalid format.');
        }

        return base64Decoded;
    }

    private decodeJwt = function (jwtToken: string) {
        
        var idTokenPartsRegex = /^([^\.\s]*)\.([^\.\s]+)\.([^\.\s]*)$/;

        var matches = idTokenPartsRegex.exec(jwtToken);
        if (!matches || matches.length < 4) {
            throw new Error(`Failed to decode Jwt token. The token has invalid format. Actual token: '${jwtToken}'`);
        }

        var crackedToken = {
            header: matches[1],
            JWSPayload: matches[2],
            JWSSig: matches[3]
        };

        return crackedToken;
    };

    private base64DecodeStringUrlSafe = function (base64IdToken: string) {
        // html5 should support atob function for decoding
        base64IdToken = base64IdToken.replace(/-/g, '+').replace(/_/g, '/');
{'fraction_non_alphanumeric': 0.12837837837837837, 'fraction_numerical': 0.017038777908343124, 'mean_word_length': 2.354679802955665, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6497096', 'n_tokens_mistral': 1025, 'n_tokens_neox': 968, 'n_words': 264}