|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenRequest.h" |
| 18 | + |
| 19 | +NS_ASSUME_NONNULL_BEGIN |
| 20 | + |
| 21 | +/** @var kRevokeTokenEndpoint |
| 22 | + @brief The endpoint for the revokeToken request. |
| 23 | + */ |
| 24 | +static NSString *const kRevokeTokenEndpoint = @"accounts:revokeToken"; |
| 25 | + |
| 26 | +/** @var kProviderIDKey |
| 27 | + @brief The key for the provider that issued the token to revoke. |
| 28 | + */ |
| 29 | +static NSString *const kProviderIDKey = @"providerId"; |
| 30 | + |
| 31 | +/** @var kTokenTypeKey |
| 32 | + @brief The key for the type of the token to revoke. |
| 33 | + */ |
| 34 | +static NSString *const kTokenTypeKey = @"tokenType"; |
| 35 | + |
| 36 | +/** @var kTokenKey |
| 37 | + @brief The key for the token to be revoked. |
| 38 | + */ |
| 39 | +static NSString *const kTokenKey = @"token"; |
| 40 | + |
| 41 | +/** @var kIDTokenKey |
| 42 | + @brief The key for the ID Token associated with this credential. |
| 43 | + */ |
| 44 | +static NSString *const kIDTokenKey = @"idToken"; |
| 45 | + |
| 46 | +typedef NS_ENUM(NSInteger, FIRTokenType) { |
| 47 | + /** Indicates that the token type is unspecified. |
| 48 | + */ |
| 49 | + FIRTokenTypeUnspecified = 0, |
| 50 | + |
| 51 | + /** Indicates that the token type is refresh token. |
| 52 | + */ |
| 53 | + FIRTokenTypeRefreshToken = 1, |
| 54 | + |
| 55 | + /** Indicates that the token type is access token. |
| 56 | + */ |
| 57 | + FIRTokenTypeAccessToken = 2, |
| 58 | + |
| 59 | + /** Indicates that the token type is authorization code. |
| 60 | + */ |
| 61 | + FIRTokenTypeAuthorizationCode = 3, |
| 62 | +}; |
| 63 | + |
| 64 | +@implementation FIRRevokeTokenRequest |
| 65 | + |
| 66 | +- (nullable instancetype)initWithToken:(NSString *)token |
| 67 | + idToken:(NSString *)idToken |
| 68 | + requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration { |
| 69 | + self = [super initWithEndpoint:kRevokeTokenEndpoint |
| 70 | + requestConfiguration:requestConfiguration |
| 71 | + useIdentityPlatform:YES |
| 72 | + useStaging:NO]; |
| 73 | + if (self) { |
| 74 | + // Apple and authorization code are the only provider and token type we support for now. |
| 75 | + // Generalize this initializer to accept other providers and token types once supported. |
| 76 | + _providerID = @"apple.com"; |
| 77 | + _tokenType = FIRTokenTypeAuthorizationCode; |
| 78 | + _token = token; |
| 79 | + _idToken = idToken; |
| 80 | + } |
| 81 | + return self; |
| 82 | +} |
| 83 | + |
| 84 | +- (nullable id)unencodedHTTPRequestBodyWithError:(NSError *__autoreleasing _Nullable *)error { |
| 85 | + NSMutableDictionary *postBody = [NSMutableDictionary dictionary]; |
| 86 | + if (_providerID) { |
| 87 | + postBody[kProviderIDKey] = _providerID; |
| 88 | + } |
| 89 | + if (_tokenType) { |
| 90 | + postBody[kTokenTypeKey] = [NSNumber numberWithInteger:_tokenType].stringValue; |
| 91 | + } |
| 92 | + if (_token) { |
| 93 | + postBody[kTokenKey] = _token; |
| 94 | + } |
| 95 | + if (_idToken) { |
| 96 | + postBody[kIDTokenKey] = _idToken; |
| 97 | + } |
| 98 | + return [postBody copy]; |
| 99 | +} |
| 100 | + |
| 101 | +@end |
| 102 | + |
| 103 | +NS_ASSUME_NONNULL_END |
0 commit comments