aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Auth/Source/RPCs/FIRVerifyAssertionRequest.m
blob: b31ae42d0d6bb05d02c782a866feb76cf7977466 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "FIRVerifyAssertionRequest.h"

#import <GoogleToolboxForMac/GTMNSData+zlib.h>
#import <GoogleToolboxForMac/GTMNSDictionary+URLArguments.h>

/** @var kVerifyAssertionEndpoint
    @brief The "verifyAssertion" endpoint.
 */
static NSString *const kVerifyAssertionEndpoint = @"verifyAssertion";

/** @var kProviderIDKey
    @brief The key for the "providerId" value in the request.
 */
static NSString *const kProviderIDKey = @"providerId";

/** @var kProviderIDTokenKey
    @brief The key for the "id_token" value in the request.
 */
static NSString *const kProviderIDTokenKey = @"id_token";

/** @var kProviderAccessTokenKey
    @brief The key for the "access_token" value in the request.
 */
static NSString *const kProviderAccessTokenKey = @"access_token";

/** @var kProviderOAuthTokenSecretKey
    @brief The key for the "oauth_token_secret" value in the request.
 */
static NSString *const kProviderOAuthTokenSecretKey = @"oauth_token_secret";

/** @var kIdentifierKey
    @brief The key for the "identifier" value in the request.
 */
static NSString *const kIdentifierKey = @"identifier";

/** @var kRequestURIKey
    @brief The key for the "requestUri" value in the request.
 */
static NSString *const kRequestURIKey = @"requestUri";

/** @var kPostBodyKey
    @brief The key for the "postBody" value in the request.
 */
static NSString *const kPostBodyKey = @"postBody";

/** @var kPendingIDTokenKey
    @brief The key for the "pendingIdToken" value in the request.
 */
static NSString *const kPendingIDTokenKey = @"pendingIdToken";

/** @var kAutoCreateKey
    @brief The key for the "autoCreate" value in the request.
 */
static NSString *const kAutoCreateKey = @"autoCreate";

/** @var kIDTokenKey
    @brief The key for the "idToken" value in the request. This is actually the STS Access Token,
        despite it's confusing (backwards compatiable) parameter name.
 */
static NSString *const kIDTokenKey = @"idToken";

/** @var kReturnSecureTokenKey
    @brief The key for the "returnSecureToken" value in the request.
 */
static NSString *const kReturnSecureTokenKey = @"returnSecureToken";

@implementation FIRVerifyAssertionRequest

- (nullable instancetype)initWithAPIKey:(NSString *)APIKey
                             providerID:(nonnull NSString *)providerID{
  self = [super initWithEndpoint:kVerifyAssertionEndpoint APIKey:APIKey];
  if (self) {
    _providerID = providerID;
    _returnSecureToken = YES;
    _autoCreate = YES;
  }
  return self;
}

- (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  NSMutableDictionary *postBody = [@{
    kProviderIDKey : _providerID,
  } mutableCopy];

  if (_providerIDToken) {
    postBody[kProviderIDTokenKey] = _providerIDToken;
  }

  if (_providerAccessToken) {
    postBody[kProviderAccessTokenKey] = _providerAccessToken;
  }

  if (!_providerIDToken && !_providerAccessToken) {
    [NSException raise:NSInvalidArgumentException
                format:@"Either IDToken or accessToken must be supplied."];
  }

  if (_providerOAuthTokenSecret) {
    postBody[kProviderOAuthTokenSecretKey] = _providerOAuthTokenSecret;
  }

  if (_inputEmail) {
    postBody[kIdentifierKey] = _inputEmail;
  }

  NSMutableDictionary *body = [@{
    kRequestURIKey : @"http://localhost", // Unused by server, but required
    kPostBodyKey : [postBody gtm_httpArgumentsString]
  } mutableCopy];

  if (_pendingIDToken) {
    body[kPendingIDTokenKey] = _pendingIDToken;
  }
  if (_accessToken) {
    body[kIDTokenKey] = _accessToken;
  }
  if (_returnSecureToken) {
    body[kReturnSecureTokenKey] = @YES;
  }

  body[kAutoCreateKey] = @(_autoCreate);

  return body;
}

@end