"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _helpers = require("./helpers");
var _token = require("./token");
const FEDAUTHINFOID = {
  STSURL: 0x01,
  SPN: 0x02
};
function readFedAuthInfo(data) {
  let offset = 0;
  let spn, stsurl;
  const countOfInfoIDs = data.readUInt32LE(offset);
  offset += 4;
  for (let i = 0; i < countOfInfoIDs; i++) {
    const fedauthInfoID = data.readUInt8(offset);
    offset += 1;
    const fedAuthInfoDataLen = data.readUInt32LE(offset);
    offset += 4;
    const fedAuthInfoDataOffset = data.readUInt32LE(offset);
    offset += 4;
    switch (fedauthInfoID) {
      case FEDAUTHINFOID.SPN:
        spn = data.toString('ucs2', fedAuthInfoDataOffset, fedAuthInfoDataOffset + fedAuthInfoDataLen);
        break;
      case FEDAUTHINFOID.STSURL:
        stsurl = data.toString('ucs2', fedAuthInfoDataOffset, fedAuthInfoDataOffset + fedAuthInfoDataLen);
        break;

      // ignoring unknown fedauthinfo options
      default:
        break;
    }
  }
  return {
    spn,
    stsurl
  };
}
function fedAuthInfoParser(buf, offset, _options) {
  let tokenLength;
  ({
    offset,
    value: tokenLength
  } = (0, _helpers.readUInt32LE)(buf, offset));
  if (buf.length < offset + tokenLength) {
    throw new _helpers.NotEnoughDataError(offset + tokenLength);
  }
  const data = buf.slice(offset, offset + tokenLength);
  offset += tokenLength;
  const {
    spn,
    stsurl
  } = readFedAuthInfo(data);
  return new _helpers.Result(new _token.FedAuthInfoToken(spn, stsurl), offset);
}
var _default = exports.default = fedAuthInfoParser;
module.exports = fedAuthInfoParser;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 5136 characters]