summaryrefslogtreecommitdiffhomepage
path: root/gui/src/shared/auth-failure.ts
blob: f8db50d82e381c3fd49bdd0096c9061083391d9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { messages } from './gettext';

export enum AuthFailureKind {
  invalidAccount,
  expiredAccount,
  tooManyConnections,
  unknown,
}

interface IAuthFailure {
  kind: AuthFailureKind;
  message: string;
}

export function parseAuthFailure(rawFailureMessage?: string): IAuthFailure {
  if (rawFailureMessage) {
    const results = /^\[(\w+)\]\s*(.*)$/.exec(rawFailureMessage);

    if (results && results.length === 3) {
      const kind = parseRawFailureKind(results[1]);
      const message = kind === AuthFailureKind.unknown ? results[2] : messageForFailureKind(kind);

      return {
        kind,
        message,
      };
    } else {
      return {
        kind: AuthFailureKind.unknown,
        message: rawFailureMessage,
      };
    }
  } else {
    return {
      kind: AuthFailureKind.unknown,
      message: messageForFailureKind(AuthFailureKind.unknown),
    };
  }
}

function parseRawFailureKind(failureId: string): AuthFailureKind {
  // These strings should match up with mullvad-types/src/auth_failed.rs
  switch (failureId) {
    case 'INVALID_ACCOUNT':
      return AuthFailureKind.invalidAccount;

    case 'EXPIRED_ACCOUNT':
      return AuthFailureKind.expiredAccount;

    case 'TOO_MANY_CONNECTIONS':
      return AuthFailureKind.tooManyConnections;

    default:
      return AuthFailureKind.unknown;
  }
}

function messageForFailureKind(kind: AuthFailureKind): string {
  switch (kind) {
    case AuthFailureKind.invalidAccount:
      return messages.pgettext(
        'auth-failure',
        'You are logged in with an invalid account number. Please log out and try another one.',
      );

    case AuthFailureKind.expiredAccount:
      return messages.pgettext('auth-failure', 'Blocking internet: account is out of time');

    case AuthFailureKind.tooManyConnections:
      return messages.pgettext(
        'auth-failure',
        'Too many simultaneous connections on this account. Disconnect another device or try connecting again shortly.',
      );

    case AuthFailureKind.unknown:
      return messages.pgettext(
        'auth-failure',
        'Unable to authenticate account. Please contact support.',
      );
  }
}