<commit_before>import { URL } from 'url';
import {Scope, ClientId, ClientRegistry} from '../oauth-types';
const Database = require('../db');


class OAuthClients {
  private clients: Map<string, Array<ClientRegistry>> = new Map();
  constructor() {
  }

  register(client: ClientRegistry) {
    if (this.clients.get(client.id)) {
      this.clients.get(client.id)!.push(client);
    } else {
      this.clients.set(client.id, [client]);
    }
  }

  get(id: string, redirectUri: URL|undefined): ClientRegistry|undefined {
    const clients = this.clients.get(id);
    if (!clients) {
      return;
    }
    if (!redirectUri) {
      return clients[0];
    }
    for (let client of clients) {
      if (client.redirect_uri.href === redirectUri.href) {
        return client;
      }
    }
    return clients[0];
  }

  async getAuthorized(userId: number): Promise<Array<ClientRegistry>> {
    let jwts = await Database.getJSONWebTokensByUser(userId);
    let authorized = new Map();

    for (let jwt of jwts) {
      let payload = JSON.parse(jwt.payload);
      if (payload.role !== 'access_token') {
        continue;
      }
      if (!this.clients.has(payload.client_id)) {
        console.warn('Orphaned access_token', jwt);
        await Database.deleteJSONWebTokenByKeyId(jwt.keyId);
        continue;
      }
      const defaultClient = this.clients.get(payload.client_id)![0];
      if (!defaultClient) {
        continue;
      }
      authorized.set(payload.client_id, defaultClient);
    }

    return Array.from(authorized.values());
  }

  async revokeClientAuthorization(userId: number, clientId: string) {
    let jwts = await Database.getJSONWebTokensByUser(userId);

    for (let jwt of jwts) {
      let payload = JSON.parse(jwt.payload);
      if (payload.client_id === clientId) {
        await Database.deleteJSONWebTokenByKeyId(jwt.keyId);
      }
    }
  }
}

let oauthClients = new OAuthClients();
oauthClients.register(
  new ClientRegistry(new URL('http://127.0.0.1:31338/callback'), 'test',
                     'Test OAuth Client', 'super secret', '/things:readwrite')
);

oauthClients.register(
  new ClientRegistry(new URL('https://gateway.localhost/oauth/local-token-service'), 'local-token',
                     'Local Token Service', 'super secret',
                     '/things:readwrite')
);
export default oauthClients;<commit_msg>Add multiple test clients to exercise new code in tests<commit_after>import { URL } from 'url';
import {Scope, ClientId, ClientRegistry} from '../oauth-types';
const Database = require('../db');


class OAuthClients {
  private clients: Map<string, Array<ClientRegistry>> = new Map();
  constructor() {
  }

  register(client: ClientRegistry) {
    if (this.clients.get(client.id)) {
      this.clients.get(client.id)!.push(client);
    } else {
      this.clients.set(client.id, [client]);
    }
  }

  get(id: string, redirectUri: URL|undefined): ClientRegistry|undefined {
    const clients = this.clients.get(id);
    if (!clients) {
      return;
    }
    if (!redirectUri) {
      return clients[0];
    }
    for (let client of clients) {
      if (client.redirect_uri.href === redirectUri.href) {
        return client;
      }
    }
    return clients[0];
  }

  async getAuthorized(userId: number): Promise<Array<ClientRegistry>> {
    let jwts = await Database.getJSONWebTokensByUser(userId);
    let authorized = new Map();

    for (let jwt of jwts) {
      let payload = JSON.parse(jwt.payload);
      if (payload.role !== 'access_token') {
        continue;
      }
      if (!this.clients.has(payload.client_id)) {
        console.warn('Orphaned access_token', jwt);
        await Database.deleteJSONWebTokenByKeyId(jwt.keyId);
        continue;
      }
      const defaultClient = this.clients.get(payload.client_id)![0];
      if (!defaultClient) {
        continue;
      }
      authorized.set(payload.client_id, defaultClient);
    }

    return Array.from(authorized.values());
  }

  async revokeClientAuthorization(userId: number, clientId: string) {
    let jwts = await Database.getJSONWebTokensByUser(userId);

    for (let jwt of jwts) {
      let payload = JSON.parse(jwt.payload);
      if (payload.client_id === clientId) {
        await Database.deleteJSONWebTokenByKeyId(jwt.keyId);
      }
    }
  }
}

let oauthClients = new OAuthClients();

oauthClients.register(
  new ClientRegistry(new URL('http://127.0.0.1:31338/callback'), 'test',
                     'Test OAuth Client', 'super secret', '/things:readwrite')
);

oauthClients.register(
  new ClientRegistry(new URL('http://127.0.0.1:31338/bonus-entry'), 'test',
                     'Test OAuth Client', 'other secret', '/things:readwrite')
);

oauthClients.register(
  new ClientRegistry(new URL('https://gateway.localhost/oauth/local-token-service'), 'local-token',
                     'Local Token Service', 'super secret',
                     '/things:readwrite')
);
export default oauthClients;
{'fraction_non_alphanumeric': 0.13716012084592144, 'fraction_numerical': 0.00785498489425982, 'mean_word_length': 2.9225908372827805, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, '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': '6170862', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1550, 'n_words': 319}