forked from jdg/oauthconsumer
-
Notifications
You must be signed in to change notification settings - Fork 9
/
OAProblem.m
165 lines (134 loc) · 3.84 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
//
// 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"
const NSString *signature_method_rejected = @"signature_method_rejected";
const NSString *parameter_absent = @"parameter_absent";
const NSString *version_rejected = @"version_rejected";
const NSString *consumer_key_unknown = @"consumer_key_unknown";
const NSString *token_rejected = @"token_rejected";
const NSString *signature_invalid = @"signature_invalid";
const NSString *nonce_used = @"nonce_used";
const NSString *timestamp_refused = @"timestamp_refused";
const NSString *token_expired = @"token_expired";
const NSString *token_not_renewable = @"token_not_renewable";
@implementation OAProblem
@synthesize problem;
- (id)initWithPointer:(const NSString *) aPointer
{
[super init];
problem = aPointer;
return self;
}
- (id)initWithProblem:(const NSString *) aProblem
{
NSUInteger idx = [[OAProblem validProblems] indexOfObject:aProblem];
if (idx == NSNotFound) {
return nil;
}
return [self initWithPointer: [[OAProblem validProblems] objectAtIndex:idx]];
}
- (id)initWithResponseBody:(const 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;
}
+ (OAProblem *)problemWithResponseBody:(const NSString *) response
{
return [[[OAProblem alloc] initWithResponseBody:response] autorelease];
}
+ (const 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:(const 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