forked from jdg/oauthconsumer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OAProblem.m
172 lines (140 loc) · 3.83 KB
/
OAProblem.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// OAProblem.m
// OAuthConsumer
//
// Created by Alberto García Hierro on 03/09/08.
// Copyright 2008 Alberto García Hierro. All rights reserved.
// bynotes.com
#import "OAProblem.h"
NSString *signature_method_rejected = @"signature_method_rejected";
NSString *parameter_absent = @"parameter_absent";
NSString *version_rejected = @"version_rejected";
NSString *consumer_key_unknown = @"consumer_key_unknown";
NSString *token_rejected = @"token_rejected";
NSString *signature_invalid = @"signature_invalid";
NSString *nonce_used = @"nonce_used";
NSString *timestamp_refused = @"timestamp_refused";
NSString *token_expired = @"token_expired";
NSString *token_not_renewable = @"token_not_renewable";
@implementation OAProblem
@synthesize problem;
- (id)initWithPointer:(NSString *) aPointer
{
if ((self = [super init])) {
problem = [aPointer copy];
}
return self;
}
- (id)initWithProblem:(NSString *) aProblem
{
NSUInteger idx = [[OAProblem validProblems] indexOfObject:aProblem];
if (idx == NSNotFound) {
return nil;
}
return [self initWithPointer: [[OAProblem validProblems] objectAtIndex:idx]];
}
- (id)initWithResponseBody:(NSString *) response
{
NSArray *fields = [response componentsSeparatedByString:@"&"];
for (NSString *field in fields) {
if ([field hasPrefix:@"oauth_problem="]) {
NSString *value = [[field componentsSeparatedByString:@"="] objectAtIndex:1];
return [self initWithProblem:value];
}
}
return nil;
}
- (void)dealloc
{
[problem release];
[super dealloc];
}
+ (OAProblem *)problemWithResponseBody:(NSString *) response
{
return [[[OAProblem alloc] initWithResponseBody:response] autorelease];
}
+ (NSArray *)validProblems
{
static NSArray *array;
if (!array) {
array = [[NSArray alloc] initWithObjects:signature_method_rejected,
parameter_absent,
version_rejected,
consumer_key_unknown,
token_rejected,
signature_invalid,
nonce_used,
timestamp_refused,
token_expired,
token_not_renewable,
nil];
}
return array;
}
- (BOOL)isEqualToProblem:(OAProblem *) aProblem
{
return [problem isEqualToString:(NSString *)aProblem->problem];
}
- (BOOL)isEqualToString:(NSString *) aProblem
{
return [problem isEqualToString:(NSString *)aProblem];
}
- (BOOL)isEqualTo:(id) aProblem
{
if ([aProblem isKindOfClass:[NSString class]]) {
return [self isEqualToString:aProblem];
}
if ([aProblem isKindOfClass:[OAProblem class]]) {
return [self isEqualToProblem:aProblem];
}
return NO;
}
- (int)code {
return [[[self class] validProblems] indexOfObject:problem];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"OAuth Problem: %@", (NSString *)problem];
}
#pragma mark class_methods
+ (OAProblem *)SignatureMethodRejected
{
return [[[OAProblem alloc] initWithPointer:signature_method_rejected] autorelease];
}
+ (OAProblem *)ParameterAbsent
{
return [[[OAProblem alloc] initWithPointer:parameter_absent] autorelease];
}
+ (OAProblem *)VersionRejected
{
return [[[OAProblem alloc] initWithPointer:version_rejected] autorelease];
}
+ (OAProblem *)ConsumerKeyUnknown
{
return [[[OAProblem alloc] initWithPointer:consumer_key_unknown] autorelease];
}
+ (OAProblem *)TokenRejected
{
return [[[OAProblem alloc] initWithPointer:token_rejected] autorelease];
}
+ (OAProblem *)SignatureInvalid
{
return [[[OAProblem alloc] initWithPointer:signature_invalid] autorelease];
}
+ (OAProblem *)NonceUsed
{
return [[[OAProblem alloc] initWithPointer:nonce_used] autorelease];
}
+ (OAProblem *)TimestampRefused
{
return [[[OAProblem alloc] initWithPointer:timestamp_refused] autorelease];
}
+ (OAProblem *)TokenExpired
{
return [[[OAProblem alloc] initWithPointer:token_expired] autorelease];
}
+ (OAProblem *)TokenNotRenewable
{
return [[[OAProblem alloc] initWithPointer:token_not_renewable] autorelease];
}
@end