|
16 | 16 | */
|
17 | 17 |
|
18 | 18 | /**
|
19 |
| - * @fileoverview Defines firebase.auth.ActionCodeUrl class which is the utility |
| 19 | + * @fileoverview Defines firebase.auth.ActionCodeURL class which is the utility |
20 | 20 | * to parse action code URLs.
|
21 | 21 | */
|
22 | 22 |
|
23 |
| -goog.provide('fireauth.ActionCodeUrl'); |
| 23 | +goog.provide('fireauth.ActionCodeURL'); |
24 | 24 |
|
| 25 | +goog.require('fireauth.ActionCodeInfo'); |
| 26 | +goog.require('fireauth.AuthError'); |
| 27 | +goog.require('fireauth.authenum.Error'); |
| 28 | +goog.require('fireauth.object'); |
25 | 29 | goog.require('goog.Uri');
|
26 | 30 |
|
27 | 31 |
|
28 | 32 | /**
|
29 | 33 | * The utility class to help parse action code URLs used for out of band email
|
30 | 34 | * flows such as password reset, email verification, email link sign in, etc.
|
31 |
| - * @param {string} actionCodeUrl The action code URL. |
| 35 | + * @param {string} actionLink The action link string. |
32 | 36 | * @constructor
|
33 | 37 | */
|
34 |
| -fireauth.ActionCodeUrl = function(actionCodeUrl) { |
35 |
| - /** @private {!goog.Uri} The action code URL components.*/ |
36 |
| - this.uri_ = goog.Uri.parse(actionCodeUrl); |
| 38 | +fireauth.ActionCodeURL = function(actionLink) { |
| 39 | + var uri = goog.Uri.parse(actionLink); |
| 40 | + var apiKey = uri.getParameterValue( |
| 41 | + fireauth.ActionCodeURL.QueryField.API_KEY) || null; |
| 42 | + var code = uri.getParameterValue( |
| 43 | + fireauth.ActionCodeURL.QueryField.CODE) || null; |
| 44 | + var mode = uri.getParameterValue( |
| 45 | + fireauth.ActionCodeURL.QueryField.MODE) || null; |
| 46 | + var operation = fireauth.ActionCodeURL.getOperation(mode); |
| 47 | + // Validate API key, code and mode. |
| 48 | + if (!apiKey || !code || !operation) { |
| 49 | + throw new fireauth.AuthError( |
| 50 | + fireauth.authenum.Error.ARGUMENT_ERROR, |
| 51 | + fireauth.ActionCodeURL.QueryField.API_KEY + ', ' + |
| 52 | + fireauth.ActionCodeURL.QueryField.CODE + 'and ' + |
| 53 | + fireauth.ActionCodeURL.QueryField.MODE + |
| 54 | + ' are required in a valid action code URL.'); |
| 55 | + } |
| 56 | + fireauth.object.setReadonlyProperties(this, { |
| 57 | + 'apiKey': apiKey, |
| 58 | + 'operation': operation, |
| 59 | + 'code': code, |
| 60 | + 'continueUrl': uri.getParameterValue( |
| 61 | + fireauth.ActionCodeURL.QueryField.CONTINUE_URL) || null, |
| 62 | + 'languageCode': uri.getParameterValue( |
| 63 | + fireauth.ActionCodeURL.QueryField.LANGUAGE_CODE) || null, |
| 64 | + 'tenantId': uri.getParameterValue( |
| 65 | + fireauth.ActionCodeURL.QueryField.TENANT_ID) || null |
| 66 | + }); |
37 | 67 | };
|
38 | 68 |
|
39 | 69 |
|
40 | 70 | /**
|
41 | 71 | * Enums for fields in URL query string.
|
42 | 72 | * @enum {string}
|
43 | 73 | */
|
44 |
| -fireauth.ActionCodeUrl.QueryField = { |
| 74 | +fireauth.ActionCodeURL.QueryField = { |
45 | 75 | API_KEY: 'apiKey',
|
46 | 76 | CODE: 'oobCode',
|
47 |
| - MODE: 'mode' |
| 77 | + CONTINUE_URL: 'continueUrl', |
| 78 | + LANGUAGE_CODE: 'languageCode', |
| 79 | + MODE: 'mode', |
| 80 | + TENANT_ID: 'tenantId' |
48 | 81 | };
|
49 | 82 |
|
50 | 83 |
|
51 | 84 | /**
|
52 |
| - * Enums for action code modes. |
53 |
| - * @enum {string} |
| 85 | + * Map of mode string to Action Code Info operation. |
| 86 | + * @const @private {!Object<string, !fireauth.ActionCodeInfo.Operation>} |
54 | 87 | */
|
55 |
| -fireauth.ActionCodeUrl.Mode = { |
56 |
| - RESET_PASSWORD: 'resetPassword', |
57 |
| - REVOKE_EMAIL: 'recoverEmail', |
58 |
| - SIGN_IN: 'signIn', |
59 |
| - VERIFY_EMAIL: 'verifyEmail' |
| 88 | +fireauth.ActionCodeURL.ModeToOperationMap_ = { |
| 89 | + 'recoverEmail': fireauth.ActionCodeInfo.Operation.RECOVER_EMAIL, |
| 90 | + 'resetPassword': fireauth.ActionCodeInfo.Operation.PASSWORD_RESET, |
| 91 | + 'signIn': fireauth.ActionCodeInfo.Operation.EMAIL_SIGNIN, |
| 92 | + 'verifyEmail': fireauth.ActionCodeInfo.Operation.VERIFY_EMAIL |
60 | 93 | };
|
61 | 94 |
|
62 | 95 |
|
63 | 96 | /**
|
64 |
| - * Returns the API key parameter of action code URL. |
65 |
| - * @return {?string} The first API key value in action code URL or |
66 |
| - * undefined if apiKey does not appear in the URL. |
| 97 | + * Maps the mode string in action code URL to Action Code Info operation. |
| 98 | + * @param {?string} mode The mode string in the URL. |
| 99 | + * @return {?fireauth.ActionCodeInfo.Operation} |
67 | 100 | */
|
68 |
| -fireauth.ActionCodeUrl.prototype.getApiKey = function() { |
69 |
| - return this.uri_.getParameterValue( |
70 |
| - fireauth.ActionCodeUrl.QueryField.API_KEY) || null; |
71 |
| -}; |
72 |
| - |
| 101 | +fireauth.ActionCodeURL.getOperation = function(mode) { |
| 102 | + if (!mode) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + return fireauth.ActionCodeURL.ModeToOperationMap_[mode] || null; |
73 | 106 |
|
74 |
| -/** |
75 |
| - * Returns the action code parameter of action code URL. |
76 |
| - * @return {?string} The first oobCode value in action code URL or |
77 |
| - * undefined if oobCode does not appear in the URL. |
78 |
| - */ |
79 |
| -fireauth.ActionCodeUrl.prototype.getCode = function() { |
80 |
| - return this.uri_.getParameterValue( |
81 |
| - fireauth.ActionCodeUrl.QueryField.CODE) || null; |
82 | 107 | };
|
83 | 108 |
|
84 | 109 |
|
85 | 110 | /**
|
86 |
| - * Returns the mode parameter of action code URL. |
87 |
| - * @return {?string} The first mode value in action code URL or |
88 |
| - * undefined if mode does not appear in the URL. |
| 111 | + * Returns an ActionCodeURL instance if the link is valid, otherwise null. |
| 112 | + * @param {string} actionLink The action code link string. |
| 113 | + * @return {?fireauth.ActionCodeURL} |
89 | 114 | */
|
90 |
| -fireauth.ActionCodeUrl.prototype.getMode = function() { |
91 |
| - return this.uri_.getParameterValue( |
92 |
| - fireauth.ActionCodeUrl.QueryField.MODE) || null; |
| 115 | +fireauth.ActionCodeURL.parseLink = function(actionLink) { |
| 116 | + try { |
| 117 | + return new fireauth.ActionCodeURL(actionLink); |
| 118 | + } catch(e) { |
| 119 | + return null; |
| 120 | + } |
93 | 121 | };
|
0 commit comments