NSData的将不接受有效的base64 EN codeD字符串 [英] NSData won't accept valid base64 encoded string
问题描述
我实施在iOS(7)cient端JSON网络令牌认证。它的工作很好。我的应用程序rceives令牌,并且可以使我的服务器进行身份验证调用它们。
现在,我希望我的客户端code检查令牌上的到期日,因此它可以知道什么时候才能重新验证。检查在JWT验证令牌的截止日期是简单的。授权令牌的base64 3 EN codeD JSON斑点,由分离'。 - 到期时间戳是在中间的blob,在一个名为字段外部
。这是自秒Unix纪元。
所以,我的code在寻找像这样:
- (*的NSDate)EXPIRATIONDATE
{
如果(_tokenAppearsValid!)回零; 如果(!_parsedExpirationDate)
{
//
//令牌为三的base64连接相隔codeD有效载荷。
//我们想要的有效载荷是中间的一个,这是一个JSON字典,以
//EXP'是到期日期的UNIX时间戳秒
//返回nil是合适的,如果没有'记录'是容易找到
// 的NSArray *成分= [self.token componentsSeparatedByString:。@]; 的NSString *有效载荷=分量[1]; NSData的* payloadJsonData = [[NSData的页头]
initWithBase64En codedString:有效载荷
选项:NSDataBase64DecodingIgnoreUnknownCharacters]; NSError * jsonError =零;
*的NSDictionary = payloadJson [NSJSONSerialization JSONObjectWithData:payloadJsonData选项:0错误:&放大器; jsonError]。
如果(payloadJson)
{
如果(payloadJson [@EXP])
{
NSTimeInterval timestampSeconds = [payloadJson [@EXP]的doubleValue];
_expirationDate = [NSDate的dateWithTimeIntervalSince1970:timestampSeconds];
}
} _parsedExpirationDate = YES;
} 返回_expirationDate;
}
问题是简单的。中间的base64一滴,当NSData的-initWithBase64En codedString解析为无
- 而这是很糟糕。
我检查中的Base64的blob,它似乎是有效的。我的服务器的返回空数据的时刻所以这里是一个例子斑点:
<$c$c>eyJlbWFpbCI6ImZvb0BiYXIuYmF6IiwiYWNjb3VudElkIjoiMTIzNDUtNjc4OTAtYmFyLWJheiIsImV4cCI6MTM5MDkxNTAzNywiaWF0IjoxMzkwOTE0MTM3fQ$c$c>
据德codeS为:
<$p$p><$c$c>{\"email\":\"foo@bar.baz\",\"accountId\":\"12345-67890-bar-baz\",\"exp\":1390915037,\"iat\":1390914137}我测试了它在这里: HTTP://www.base64de$c$c.org
我用的NSData的方法的base64在elswhere我的应用程序的成功 - 我不认为我在做什么特别在这里打破。但我所有的耳朵!任何想法?
您Base64编码字符串无效。它必须以 =
字符都被填充
的长度是4的倍数在您的情况:eyJlbWFp .... MTM3fQ ==
通过这种填充, initWithBase64En codedString
德codeS中的Base64字符串正确。
I'm implementing JSON Web Token authentication on the iOS (7) cient-side. It's working nicely. My app rceives tokens, and can make authenticated calls to my server with them.
Now, I want my client side code to check for an expiration date on the token so it can know when to re-authenticate. Checking for the expiration date on a JWT auth token is straightforward. The authorization token is 3 base64 encoded JSON blobs, separated by a '.' - The expiration timestamp is in the middle blob, in a field called ext
. It's seconds since unix epoch.
So my code's looking like so:
- (NSDate*) expirationDate
{
if ( !_tokenAppearsValid ) return nil;
if ( !_parsedExpirationDate )
{
//
// Token is three base64 encoded payloads separated by '.'
// The payload we want is the middle one, which is a JSON dict, with
// 'exp' being the unix seconds timestamp of the expiration date
// Returning nil is appropriate if no 'exp' is findable
//
NSArray *components = [self.token componentsSeparatedByString:@"."];
NSString *payload = components[1];
NSData* payloadJsonData = [[NSData alloc]
initWithBase64EncodedString:payload
options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSError* jsonError = nil;
NSDictionary* payloadJson = [NSJSONSerialization JSONObjectWithData:payloadJsonData options:0 error:&jsonError];
if ( payloadJson )
{
if ( payloadJson[@"exp"] )
{
NSTimeInterval timestampSeconds = [payloadJson[@"exp"] doubleValue];
_expirationDate = [NSDate dateWithTimeIntervalSince1970:timestampSeconds];
}
}
_parsedExpirationDate = YES;
}
return _expirationDate;
}
The problem is simple. The middle base64 blob, when parsed by NSData -initWithBase64EncodedString is nil
- and that's bad.
I've checked the base64 blob and it seems to be valid. My server's returning dummy data for the moment so here's an example blob:
eyJlbWFpbCI6ImZvb0BiYXIuYmF6IiwiYWNjb3VudElkIjoiMTIzNDUtNjc4OTAtYmFyLWJheiIsImV4cCI6MTM5MDkxNTAzNywiaWF0IjoxMzkwOTE0MTM3fQ
It decodes to:
{"email":"foo@bar.baz","accountId":"12345-67890-bar-baz","exp":1390915037,"iat":1390914137}
I tested it here: http://www.base64decode.org
I've used NSData's base64 methods elswhere in my app with success - I don't think I'm doing anything particularly broken here. But I'm all ears! Any ideas?
Your Base64 string is not valid. It must be padded with =
characters to have
a length that is a multiple of 4. In your case: "eyJlbWFp....MTM3fQ=="
.
With this padding, initWithBase64EncodedString
decodes the Base64 string correctly.
这篇关于NSData的将不接受有效的base64 EN codeD字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!