Skip to content

Commit

Permalink
Fixed issue with '_' and '-' characters in Base64.
Browse files Browse the repository at this point in the history
  • Loading branch information
hzalaz committed Oct 16, 2014
1 parent 8b25554 commit 6b9eb75
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Example/Tests/A0JWTDecoderSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#import "Specta.h"
#import "A0JWTDecoder.h"

#define kJWTWithSpecialChars @"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1M2VjNjdkODI1NDIyMzE3MDAzNGYxNzciLCJlbWFpbCI6Im15ZW1haWxAZ21haWwuY29tIiwicHJvZmlsZV9waWN0dXJlIjoiaHR0cDovL2Nkbi51YnVidWJ0ZXN0ZXIuaW8vN2Q4MjU0MjIzMTcwMDM0ZjE3Ny5wbmc_dD0xNDEzNDQyMTI4OTAwIiwiaWF0IjoxNDEzNDcyNzUyfQ.WEELiFZqavfxPcnZ7vz44gbPMbEc0xeCaaS53btTYXY"

NSString *A0TokenJWTCreate(NSDate *expireDate) {
NSString *claims = [NSString stringWithFormat:@"{\"exp\": %f}", expireDate.timeIntervalSince1970];
Expand Down Expand Up @@ -136,6 +137,21 @@
});
});

context(@"valid id_token with special chars", ^{

beforeEach(^{
payload = [A0JWTDecoder payloadOfJWT:kJWTWithSpecialChars error:&error];
});

specify(@"payload", ^{
expect(payload).notTo.beEmpty();
});

specify(@"no error", ^{
expect(error).to.beNil();
});
});

sharedExamplesFor(@"invalid id_token", ^(NSDictionary *data) {

beforeEach(^{
Expand All @@ -150,13 +166,13 @@
specify(@"an error", ^{
expect(error).toNot.beNil();
});

});

itShouldBehaveLike(@"invalid id_token", @{ @"id_token": @"NOPARTS" });
itShouldBehaveLike(@"invalid id_token", @{ @"id_token": @"NOTENOUGH.PARTS" });
itShouldBehaveLike(@"invalid id_token", @{ @"id_token": @"HEADER.INVALIDCLAIM.SIGNATURE" });
});
});

SpecEnd
SpecEnd
3 changes: 3 additions & 0 deletions Pod/Classes/A0JWTDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ + (NSDictionary *)payloadOfJWT:(NSString *)jwt error:(NSError *__autoreleasing *
NSInteger requiredLength = (4 * ceil((double)claimsLength / 4.0));
NSInteger paddingCount = requiredLength - claimsLength;

claimsBase64 = [claimsBase64 stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
claimsBase64 = [claimsBase64 stringByReplacingOccurrencesOfString:@"_" withString:@"/"];

if (paddingCount > 0) {
NSString *padding =
[[NSString string] stringByPaddingToLength:paddingCount
Expand Down

0 comments on commit 6b9eb75

Please sign in to comment.