<commit_before>import { resolve, reject } from 'rsvp';
import { observer } from '@ember/object';
import { alias } from '@ember/object/computed';
import { on } from '@ember/object/evented';
import Service, { inject as service } from '@ember/service';
import C from 'shared/utils/constants';
import {  get, set } from '@ember/object';
import { isArray } from '@ember/array';

export default Service.extend({
  cookies: service(),
  session: service(),
  github:  service(),
  shibbolethAuth: service(),
  globalStore: service(),
  clusterStore: service(),
  projectStore: service('store'),
  app: service(),

  loadedVersion: null,
  me: null,

  // These are set by authenticated/route
  // Is access control enabled
  enabled: true, // @TODO-2.0 remove this, always enabled

  // What kind of access control
  provider: null,  // @TODO-2.0 remove this, and how do i check?
  providers: null,

  // Are you an admin
  admin: alias('me.hasAdmin'),

  // The identity from the session isn't an actual identity model...
  identity: null,
  identityObsvr: on('init', observer(`session.${C.SESSION.IDENTITY}`, function() {
    var obj = this.get(`session.${C.SESSION.IDENTITY}`) || {};
    obj.type = 'identity';
    this.set('identity', this.get('globalStore').createRecord(obj));
  })),


  testAuth() {
    // make a call to api base because it is authenticated
    return this.get('globalStore').rawRequest({
      url: '',
    }).then((/*xhr*/) => {
//      let loaded = this.get('loadedVersion');
//      let cur = xhr.headers.get(C.HEADER.RANCHER_VERSION);

      // Reload if the version changes
//      if ( loaded && cur && loaded !== cur ) {
//        window.location.href = window.location.href;
//        return;
//      }

      // Auth token still good
      return resolve('Auth Succeeded');
    }, (/* err */) => {
      // Auth token expired
      return reject('Auth Failed');
    });
  },

  detect() {
    const self = this;
    const globalStore = get(this,'globalStore');

    return globalStore.request({
      url: '/v3-public/authProviders',
    }).then((providers) => {

      if ( providers && get(providers,'length') ) {

        set(this, 'providers', providers);
        if (get(providers, 'length') === 1) {
          set(this, 'provider', get(providers, 'firstObject.id'));
        }
      } else {
        set(this, 'providers', []);
      }
      return done();

    }).catch(() => {

      set(this, 'providers', []);
      return done();
    });

    function done() {
      return globalStore.request({
        url: 'users?me=true',
      })
      .then((users) => {
        if ( get(users, 'length') ) {
          set(self, 'me', get(users,'firstObject'));
        }

        return resolve(undefined, `API supports authentication`);

        // // If we get a good response back, the API supports authentication
        // var token = xhr.body.data[0];

        // self.setProperties({
        //   'loadedVersion': xhr.headers.get(C.HEADER.RANCHER_VERSION), // TODO 2.0 do we still need the version?
        // });
      })
      .catch((err) => {
        // Otherwise this API is too old to do auth, or something else went wrong since that's impossible in 2.0
        self.set('app.initError', err);
        return resolve(undefined,'Error determining API authentication');
      });
    }
  },

  shibbolethConfigured(token) {
    let rv = false;
    if ((token.authProvider||'') === 'shibbolethconfig' && token.userIdentity) {
      rv = true;
    }
    return rv;
  },

  login(providerId, body) {
    body.description = 'UI Session';
    body.responseType = 'cookie';
    body.ttl = 16*60*60*1000;
    body.labels = {
      'ui-session': 'true',
    };

    let url;
    let provider = (get(this,'providers')||[]).findBy('id', providerId);
    if ( provider ) {
      url = provider.actionLinks.login;
    } else {
      return reject({type: 'error', message: 'Provider config not found'});
    }

    console.log('Logging into',url);
    let req = this.get('globalStore').rawRequest({
      method: 'POST',
      url: url,
      data: body,
    }).then(() => {
      return this.get('globalStore').find('user', null, {forceReload: true, filter: {me: true}})
        .then((users) => {
          let me = get(users,'firstObject');
          set(this, 'me', me);
          return me;
        })
        .catch((res) => {
          let err;
          try {
            err = res.body;
          } catch(e) {
            console.log('Error 1 logging in', e, res);
            err = {type: 'error', message: 'Error 1 logging in'};
          }
          return reject(err);
        });
    }).catch((res) => {
      let err;
      try {
        err = res.body;
      } catch(e) {
        console.log('Error 2 logging in', e, res);
        err = {type: 'error', message: 'Error logging in'};<commit_msg>Show errors on login<commit_after>import { resolve, reject } from 'rsvp';
import { observer } from '@ember/object';
import { alias } from '@ember/object/computed';
import { on } from '@ember/object/evented';
import Service, { inject as service } from '@ember/service';
import C from 'shared/utils/constants';
import {  get, set } from '@ember/object';
import { isArray } from '@ember/array';

export default Service.extend({
  cookies: service(),
  session: service(),
  github:  service(),
  shibbolethAuth: service(),
  globalStore: service(),
  clusterStore: service(),
  projectStore: service('store'),
  app: service(),

  me: null,

  // These are set by authenticated/route
  // Is access control enabled
  enabled: true, // @TODO-2.0 remove this, always enabled

  // What kind of access control
  provider: null,  // @TODO-2.0 remove this, and how do i check?
  providers: null,

  // Are you an admin
  admin: alias('me.hasAdmin'),

  // The identity from the session isn't an actual identity model...
  identity: null,
  identityObsvr: on('init', observer(`session.${C.SESSION.IDENTITY}`, function() {
    var obj = this.get(`session.${C.SESSION.IDENTITY}`) || {};
    obj.type = 'identity';
    this.set('identity', this.get('globalStore').createRecord(obj));
  })),


  testAuth() {
    // make a call to api base because it is authenticated
    return this.get('globalStore').rawRequest({
      url: '',
    }).then((/*xhr*/) => {
      // Auth token still good
      return resolve('Auth Succeeded');
    }, (/* err */) => {
      // Auth token expired
      return reject('Auth Failed');
    });
  },

  detect() {
    const globalStore = get(this,'globalStore');

    return globalStore.request({url: '/v3-public/authProviders'}).then((providers) => {
      if ( providers && get(providers,'length') ) {
        set(this, 'providers', providers);
        if (get(providers, 'length') === 1) {
          set(this, 'provider', get(providers, 'firstObject.id'));
        }
      } else {
        set(this, 'providers', []);
      }

      this.loadMe();
    }).catch((err) => {
      set(this, 'app.initError', err);
      set(this, 'providers', []);
    });
  },

  loadMe() {
    const globalStore = get(this,'globalStore');
    return globalStore.request({url: 'users?me=true'}).then((users) => {
      const me = get(users, 'firstObject');
      set(this, 'me', me);
      return me;
    });
  },

  shibbolethConfigured(token) {
    let rv = false;
    if ((token.authProvider||'') === 'shibbolethconfig' && token.userIdentity) {
      rv = true;
    }
    return rv;
  },

  login(providerId, body) {
    body.description = 'UI Session';
    body.responseType = 'cookie';
    body.ttl = 16*60*60*1000;
    body.labels = {
      'ui-session': 'true',
    };

    let url;
    let provider = (get(this,'providers')||[]).findBy('id', providerId);
    if ( provider ) {
      url = provider.actionLinks.login;
    } else {
      return reject({type: 'error', message: 'Provider config not found'});
    }

    console.log('Logging into',url);
    let req = this.get('globalStore').rawRequest({
      method: 'POST',
      url: url,
      data: body,
    }).then(() => {
      return this.loadMe()
        .catch((res) => {
          let err;
          try {
            err = res.body;
          } catch(e) {
            console.log('Error loading user', e, res);
            err = {type: 'error', message: 'Error loading user'};
          }
          return reject(err);
        });
    }).catch((res) => {
      let err;
      try {
        err = res.body;
      } catch(e) {
        console.log('Error logging in', e, res);
        err = {type: 'error', message: 'Error logging in'};
{'fraction_non_alphanumeric': 0.16922356247804193, 'fraction_numerical': 0.004684389272748566, 'mean_word_length': 2.4701340918325885, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '6419161', 'n_tokens_mistral': 2669, 'n_tokens_neox': 2601, 'n_words': 686}