forked from Aufree/PHPHub-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccessTokenHandler.m
79 lines (66 loc) · 3.24 KB
/
AccessTokenHandler.m
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
//
// AccessTokenHandler.m
// PHPHub
//
// Created by Aufree on 9/21/15.
// Copyright (c) 2015 ESTGroup. All rights reserved.
//
#import "AccessTokenHandler.h"
#import "SSKeychain.h"
@implementation AccessTokenHandler
#pragma mark - Client Grant
+ (NSString *)getClientGrantAccessTokenFromLocal {
NSString *token = [GVUserDefaults standardUserDefaults].userClientToken;
return [NSString stringWithFormat:@"Bearer %@", token];
}
+ (void)storeClientGrantAccessToken:(NSString *)token {
[GVUserDefaults standardUserDefaults].userClientToken = token;
[[BaseApi clientGrantInstance] setUpClientGrantRequest];
}
+ (void)fetchClientGrantToken {
NSURL *url = [NSURL URLWithString:APIBaseURL];
AFOAuth2Client *oauthClient = [AFOAuth2Client clientWithBaseURL:url clientID:Client_id secret:Client_secret];
[oauthClient authenticateUsingOAuthWithURLString:APIAccessTokenURL
scope:@""
success: ^(AFOAuthCredential *credential) {
NSLog(@"oauthClient -- > I have a CLIENT GRANT token! %@", credential.accessToken);
[AccessTokenHandler storeClientGrantAccessToken:credential.accessToken];
}
failure: ^(NSError *error) {
NSLog(@" oauthClient --> Error: %@", error);
}];
}
+ (void)fetchClientGrantTokenWithRetryTimes:(NSInteger)times callback:(BaseResultBlock)block {
NSURL *url = [NSURL URLWithString:APIBaseURL];
AFOAuth2Client *oauthClient = [AFOAuth2Client clientWithBaseURL:url clientID:Client_id secret:Client_secret];
[oauthClient authenticateUsingOAuthWithURLString:APIAccessTokenURL
scope:@""
success: ^(AFOAuthCredential *credential) {
NSLog(@"oauthClient -- > I have a CLIENT GRANT token! %@", credential.accessToken);
[AccessTokenHandler storeClientGrantAccessToken:credential.accessToken];
if (block) block(@{@"access_token": credential.accessToken}, nil);
}
failure: ^(NSError *error) {
if (times > 0) {
NSInteger newRetryTime = times - 1;
[self fetchClientGrantTokenWithRetryTimes:newRetryTime callback:block];
} else {
if (block) block(nil, error);
}
NSLog(@" oauthClient --> Error: %@", error);
}];
}
#pragma mark - Password Grant
+ (NSString *)getLoginTokenGrantAccessToken {
NSString *token = [SSKeychain passwordForService:KeyChainService account:KeyChainAccount];
return [NSString stringWithFormat:@"Bearer %@", token];
}
+ (void)storeLoginTokenGrantAccessToken:(NSString *)token {
[SSKeychain setPassword:token forService:KeyChainService account:KeyChainAccount];
[[BaseApi loginTokenGrantInstance] setUpLoginTokenGrantRequest];
}
+ (void)clearToken {
[SSKeychain deletePasswordForService:KeyChainService account:KeyChainAccount];
[GVUserDefaults standardUserDefaults].userClientToken = nil;
}
@end