|
| 1 | +// |
| 2 | +// Copyright 2025 The Matrix.org Foundation C.I.C |
| 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 "MXAuthMetadata.h" |
| 18 | + |
| 19 | +static NSString *const kMXIssuer = @"issuer"; |
| 20 | +static NSString *const kMXAccountManagementUri = @"account_management_uri"; |
| 21 | +static NSString *const kMXAccountManagementActionsSupported = @"account_management_actions_supported"; |
| 22 | + |
| 23 | +@interface MXAuthMetadata () |
| 24 | + |
| 25 | +@property (nonatomic, readwrite) NSString *issuer; |
| 26 | +@property (nonatomic, readwrite, nullable) NSString *accountManagementUri; |
| 27 | +@property (nonatomic, readwrite, nullable) NSArray<NSString*> *accountManagementActionsSupported; |
| 28 | + |
| 29 | +@end |
| 30 | + |
| 31 | +@implementation MXAuthMetadata |
| 32 | + |
| 33 | ++ (instancetype)modelFromJSON:(NSDictionary *)JSONDictionary |
| 34 | +{ |
| 35 | + MXAuthMetadata *oauthMetadata; |
| 36 | + |
| 37 | + NSString *issuer; |
| 38 | + MXJSONModelSetString(issuer, JSONDictionary[kMXIssuer]); |
| 39 | + |
| 40 | + if (issuer) |
| 41 | + { |
| 42 | + oauthMetadata = [[MXAuthMetadata alloc] init]; |
| 43 | + oauthMetadata.issuer = issuer; |
| 44 | + MXJSONModelSetString(oauthMetadata.accountManagementUri, JSONDictionary[kMXAccountManagementUri]) |
| 45 | + MXJSONModelSetArray(oauthMetadata.accountManagementActionsSupported, JSONDictionary[kMXAccountManagementActionsSupported]) |
| 46 | + } |
| 47 | + |
| 48 | + return oauthMetadata; |
| 49 | +} |
| 50 | + |
| 51 | +-(NSURL * _Nullable) getLogoutDeviceURLFromID: (NSString * ) deviceID |
| 52 | +{ |
| 53 | + if (!_accountManagementUri) |
| 54 | + { |
| 55 | + return nil; |
| 56 | + } |
| 57 | + NSURLComponents *components = [NSURLComponents componentsWithString:_accountManagementUri]; |
| 58 | + // default to the stable value |
| 59 | + NSString *actionParam = @"org.matrix.device_delete"; |
| 60 | + if ([_accountManagementActionsSupported containsObject: @"org.matrix.delete_device"]) { |
| 61 | + // use the stable value |
| 62 | + } else if ([_accountManagementActionsSupported containsObject: @"org.matrix.session_end"]) { |
| 63 | + // earlier version of MSC4191 |
| 64 | + actionParam = @"org.matrix.session_end"; |
| 65 | + } else if ([_accountManagementActionsSupported containsObject: @"session_end"]) { |
| 66 | + // previous unspecced version |
| 67 | + actionParam = @"session_end"; |
| 68 | + } |
| 69 | + |
| 70 | + components.queryItems = @[ |
| 71 | + [NSURLQueryItem queryItemWithName:@"device_id" value:deviceID], |
| 72 | + [NSURLQueryItem queryItemWithName:@"action" value:actionParam] |
| 73 | + ]; |
| 74 | + return components.URL; |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +#pragma mark - NSCoding |
| 79 | + |
| 80 | +- (id)initWithCoder:(NSCoder *)aDecoder |
| 81 | +{ |
| 82 | + self = [super init]; |
| 83 | + if (self) |
| 84 | + { |
| 85 | + _issuer = [aDecoder decodeObjectForKey:kMXIssuer]; |
| 86 | + _accountManagementUri = [aDecoder decodeObjectForKey: kMXAccountManagementUri]; |
| 87 | + _accountManagementActionsSupported = [aDecoder decodeObjectForKey: kMXAccountManagementActionsSupported]; |
| 88 | + } |
| 89 | + return self; |
| 90 | +} |
| 91 | + |
| 92 | +- (void)encodeWithCoder:(NSCoder *)aCoder |
| 93 | +{ |
| 94 | + [aCoder encodeObject:_issuer forKey:kMXIssuer]; |
| 95 | + [aCoder encodeObject:_accountManagementUri forKey:kMXAccountManagementUri]; |
| 96 | + [aCoder encodeObject:_accountManagementActionsSupported forKey:kMXAccountManagementActionsSupported]; |
| 97 | +} |
| 98 | + |
| 99 | +@end |
0 commit comments