var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import './tap-encryption-extension';
import '@iotize/tap/service/impl/group';
import '@iotize/tap/service/impl/interface';
import { bufferToHexString } from '@iotize/common/byte-converter';
import { KaitaiStreamWriter } from '@iotize/common/byte-stream';
import { StringConverter } from '@iotize/tap/client/impl';
import { hmacSHA256, pbkdf2 } from '@iotize/tap/crypto';
import { debug } from './debug';
import { TapAuthError } from './tap-auth-error';
import { hashLoginPassword, XOR } from './utility';
const TAG = 'Scram';
export function DEFAULT_SCRAM_SALT_GENERATOR() {
    const data = new Array(ScramAuth.USER_SALT_SIZE).fill(0).map((_) => {
        return Math.floor(Math.random() * 0xff);
    });
    // To be 100% sure that salt will never be [0,0,0,0] which will disable login for the user
    if (data[0] === 0) {
        data[0] = 1;
    }
    return Uint8Array.from(data);
}
export class ScramAuth {
    // protected _sessionState!: BehaviorSubject<SessionState>;
    // /**
    //  * Listen to session state changed
    //  */
    // public get sessionState(): Observable<SessionState> {
    //     return this._sessionState.asObservable();
    // }
    // /**
    //  * Get current session state
    //  */
    // public get sessionStateSnapshot(): SessionState {
    //     return this._sessionState.value;
    // }
    constructor(tap) {
        this.tap = tap;
        this.nonceGenerator = () => {
            return Math.floor(Math.random() * 0xffffffff);
        };
        this.saltGenerator = DEFAULT_SCRAM_SALT_GENERATOR;
        // this.sessionData = {};
    }
    // async changeCurrentUserPassword(newPassword: string): Promise<void> {
    //     let groupId = (await this.device.service.interface.getCurrentGroupId()).body()!;
    //     if (!this.sessionData.options || !groupId){
    //         throw new AuthError(AuthError.Code.NOT_LOGGED_IN, 'Cannot change password, your are not logged in');
    //     }
    //     let newPasswordKey: Uint8Array = ScramAuth.createScramPasswordKey(newPassword, this.sessionData.options);
    //     (await this.device.service.group.changePasswordKey(groupId, newPasswordKey)).successful();
    // }
    // /**
    //  * Enable/Disable scram for this tap
    //  * @param enabled
    //  */
    // async enable(enabled = true): Promise<void> {
    //     const lockInfo = (await this.device.service.interface.getSecurityOptions()).body();
    //     if (lockInfo.scramActivated === enabled) {
    //         return;
    //     }
    //     lockInfo.scramActivated = enabled;
    //     if (enabled) {
    //         lockInfo.hashPassword = true;
    //     }
    //     (await this.device.service.interface.putSecurityOptions(lockInfo)).successful();
    // }
    changePassword(newPassword, groupId, salt = this.saltGenerator()) {
        return __awaiter(this, void 0, void 0, function* () {
            const options = {
                iterationNumber: (yield this.tap.service.scram.getHashIteration()).body(),
                salt,
            };
            const newPasswordKey = ScramAuth.createScramPasswordKey(newPassword, options);
            debug(TAG, `Changing password key: ${bufferToHexString(newPasswordKey)} salt=${bufferToHexString(salt)}; iteration=${options.iterationNumber} for user id "${groupId}"`);
            (yield this.tap.service.group.changePasswordKey(groupId, newPasswordKey)).successful();
        });
    }
    /**
     * Perform login
     *
     * @param params
     *
     * @throws Error if scram is not activated
     */
    login(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const clientNonce = this.generateNonce();
            debug(TAG, 'clientNonce', clientNonce);
            const loginParams = {
                username: params.username,
                clientNonce,
            };
            const loginBody = (yield this.tap.service.scram.login(loginParams)).body();
            const keys = ScramAuth.computeKeys(params, loginBody, clientNonce);
            const sessionData = {};
            sessionData.storedKey = keys.storedKey;
            sessionData.serverKey = keys.serverKey;
            const serverNonce = loginBody.serverNonce;
            const deviceServerProof = (yield this.tap.service.scram.loginProof(keys.clientProof)).body();
            const expectedServerProof = keys.serverProof;
            // TODO maybe we can find something better to test array equals ...
            if (bufferToHexString(expectedServerProof) !==
                bufferToHexString(deviceServerProof)) {
                throw new TapAuthError.InvalidServerKey(expectedServerProof, deviceServerProof);
            }
            sessionData.clientNonce = clientNonce;
            sessionData.key = ScramAuth.computeSessionKey(clientNonce, serverNonce, loginBody.salt, keys.serverKey, keys.storedKey);
            return sessionData;
        });
    }
    static createScramPasswordKey(newPassword, options) {
        const keys = ScramAuth.computeBaseKeys(newPassword, options);
        const saltCopy = new Uint8Array(options.salt);
        const buffer = KaitaiStreamWriter.create(ScramAuth.SCRAM_PASSWORD_LENGTH);
        buffer
            .writeBytes(keys.storedKey, ScramAuth.KEY_SIZE)
            .writeBytes(keys.serverKey, ScramAuth.KEY_SIZE)
            .writeBytes(saltCopy, ScramAuth.USER_SALT_SIZE);
        return buffer.toBytes;
    }
    static computeBaseKeys(password, options) {
        const hashedPassword = hashLoginPassword(password);
        // debug(TAG, 'ScramAuth', 'hashedPassword', bufferToHexString(hashedPassword));
        const saltedPassword = ScramAuth.saltedPassword(hashedPassword, options.salt, options.iterationNumber);
        // debug(TAG, 'ScramAuth', 'saltedPassword', bufferToHexString(saltedPassword));
        // let clientKey: Uint8Array = ScramAuth.clientKey(saltedPassword);
        // debug(TAG, 'ScramAuth', 'clientKey', bufferToHexString(clientKey));
        const storedKey = ScramAuth.storedKey(saltedPassword);
        debug(TAG, 'storeKey', bufferToHexString(storedKey));
        const serverKey = ScramAuth.serverKey(saltedPassword);
        debug(TAG, 'serverKey', bufferToHexString(serverKey));
        return {
            hashedPassword,
            saltedPassword,
            storedKey,
            serverKey,
        };
    }
    static computeKeys(credentials, loginBody, clientNonce) {
        const keys = ScramAuth.computeBaseKeys(credentials.password, loginBody);
        const clientProof = ScramAuth.clientProof(keys.storedKey, clientNonce, loginBody.serverNonce);
        // debug(TAG, 'ScramAuth', 'clientProof', bufferToHexString(clientProof));
        const serverProof = ScramAuth.serverProof(keys.serverKey, clientNonce, loginBody.serverNonce);
        // debug(TAG, 'ScramAuth', 'serverProof', bufferToHexString(serverProof));
        return Object.assign(Object.assign({}, keys), { clientProof,
            serverProof });
    }
    logout() {
        return __awaiter(this, void 0, void 0, function* () {
            const response = this.tap.service.interface.logout();
            this.tap.encryption.stop();
            return response;
        });
    }
    static clientProof(storedKey, clientNonce, serverNonce) {
        return ScramAuth.computeProof(storedKey, clientNonce, serverNonce).subarray(0, ScramAuth.KEY_SIZE);
    }
    static serverProof(serverKey, clientNonce, serverNonce) {
        return ScramAuth.computeProof(serverKey, serverNonce, clientNonce).subarray(0, ScramAuth.KEY_SIZE);
    }
    // getSessionKey(): Uint8Array {
    //     if (!this.sessionData.key) {
    //         throw new Error('Session not started');
    //     }
    //     return this.sessionData.key;
    // }
    generateNonce() {
        return this.nonceGenerator();
    }
    /**
     * SaltedPwd = PBKDF2 ( HashedPassword, UserSalt, ItCnt )
     * @param password
     * @param userSalt
     * @param iteration
     */
    static saltedPassword(hashedPassword, userSalt, iterations) {
        return pbkdf2(hashedPassword, userSalt, iterations);
    }
    /**
     * ClientKey = HMAC ( SaltedPwd | « ClientKey »)
     * @param saltedPassword
     */
    // public static clientKey(saltedPassword: InputDataType): Uint8Array{
    //     let encodedLabel = ScramAuth.encodeLabel(ScramAuth.CLIENT_KEY_LABEL);
    //     let encodedLabel2 : Uint8Array = KaitaiStreamWriter.mergeArrays(
    //         encodedLabel,
    //         Uint8Array.from([0])
    //     ).data;
    //     // debug(TAG, 'ScramAuth', 'clientKey()', 'data => ', bufferToHexString(data), 'len', data.length, `(${saltedPassword.length} + ${encodedLabel.length})`);
    //     return ScramAuth.HMAC(
    //         saltedPassword,
    //         // userSalt
    //         encodedLabel2
    //     ).subarray(0, ScramAuth.KEY_SIZE);
    // }
    /**
     * StoredKey = H ( ClientKey )
     * @param saltedPassword
     */
    static storedKey(saltedPassword) {
        return ScramAuth.HASH(saltedPassword, ScramAuth.CLIENT_KEY_LABEL, ScramAuth.CLIENT_KEY_ITERATION_NUMBER);
    }
    static serverKey(saltedPassword) {
        return ScramAuth.HASH(saltedPassword, ScramAuth.SERVER_KEY_LABEL, ScramAuth.SERVER_KEY_ITERATION_NUMBER);
    }
    static HASH(key, label, iteration) {
        return pbkdf2(key, label, iteration, ScramAuth.KEY_SIZE / 4);
    }
    /**
     * ClientSignature = HMAC ( StoredKey | ClientNonce | ServerNonce )
     * @param key
     * @param nonce1
     * @param nonce2
     */
    static computeProof(key, nonce1, nonce2) {
        const buffer = KaitaiStreamWriter.create(key.length + ScramAuth.CLIENT_NONCE_SIZE + ScramAuth.SERVER_NONCE_SIZE);
        buffer
            .writeU(nonce1, ScramAuth.CLIENT_NONCE_SIZE)
            .writeBytes(key)
            .writeU(nonce2, ScramAuth.SERVER_NONCE_SIZE);
        return hmacSHA256(buffer.toBytes, key);
    }
    /**
     * ClientProofCheck = StoredKey ^ ClientProof
     * @param storedKey
     * @param clientProof
     */
    static clientProofCheck(storedKey, clientProof) {
        return XOR(storedKey, clientProof);
    }
    /**
     * CommunicationKey = H ( ClientNonce | ServerNonce | StoredKey | « CommunicationKey » )
     * @param clientNonce
     * @param serverNonce
     * @param storedKey
     */
    static computeSessionKey(clientNonce, serverNonce, userSalt, serverKey, storedKey) {
        const encodedLabel = ScramAuth.encodeLabel(ScramAuth.COMMUNICATION_KEY_LABEL);
        const buffer = KaitaiStreamWriter.create(ScramAuth.CLIENT_NONCE_SIZE +
            ScramAuth.SERVER_NONCE_SIZE +
            userSalt.length +
            serverKey.length +
            storedKey.length
        // + encodedLabel.length
        );
        buffer
            .writeU(clientNonce, ScramAuth.CLIENT_NONCE_SIZE)
            .writeBytes(serverKey)
            .writeBytes(userSalt)
            .writeBytes(storedKey)
            .writeU(serverNonce, ScramAuth.SERVER_NONCE_SIZE);
        // .add(encodedLabel)
        return hmacSHA256(buffer.toBytes, serverKey).subarray(0, ScramAuth.KEY_SIZE);
    }
    /**
     *
     * @param input
     */
    static encodeLabel(input) {
        return this.stringConverter.encode(input);
    }
}
ScramAuth.CRC_LENGTH = 4;
ScramAuth.CLIENT_NONCE_SIZE = 4;
ScramAuth.SERVER_NONCE_SIZE = 4;
ScramAuth.ITERATION_NUMBER_SIZE = 4;
// public sessionData: ScramAuth.SessionData;
ScramAuth.COMMUNICATION_KEY_LABEL = 'CommunicationKey';
// static CLIENT_PROOF_LABEL: string = "ClientProof";
ScramAuth.CLIENT_KEY_LABEL = 'ClientKey';
ScramAuth.SERVER_KEY_LABEL = 'ServerKey';
ScramAuth.KEY_SIZE = 16;
ScramAuth.CLIENT_KEY_ITERATION_NUMBER = 2;
ScramAuth.SERVER_KEY_ITERATION_NUMBER = 2;
ScramAuth.USER_SALT_SIZE = 4;
ScramAuth.SCRAM_PASSWORD_LENGTH = ScramAuth.KEY_SIZE * 2 + ScramAuth.USER_SALT_SIZE;
ScramAuth.stringConverter = StringConverter.ascii();
//# sourceMappingURL=data:application/json;base64, 