Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 32 additions & 137 deletions Demo/CMPComapiFoundation.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


import UIKit
import CMPComapiFoundation

class AppConfigurator: NSObject {
let window: UIWindow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,62 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

import JWT
import Foundation
import CommonCrypto

class JWTokenGenerator {

struct AuthHeaders {
static let HeaderType = "JWT"
}

static func generate(tokenFor nonce: String, profileId: String, issuer: String, audience: String, secret: String) -> String {
let now = Date()
let exp = Calendar.current.date(byAdding: .day, value: 30, to: now)!

let base64SecretKey = secret.data(using: .utf8)!

let headers = ["typ" : NSString.init(string: AuthHeaders.HeaderType)] as [AnyHashable : Any]

let claims = ["nonce" : NSString.init(string: nonce),
"sub" : NSString.init(string: profileId),
"iss" : NSString.init(string: issuer),
"aud" : NSString.init(string: audience),
"iat" : NSNumber(value: now.timeIntervalSince1970),
"exp" : NSNumber(value: exp.timeIntervalSince1970)] as [AnyHashable : Any]

let algorithm = JWTAlgorithmFactory.algorithm(byName: "HS256")

let e = JWTBuilder.encodePayload(claims)!

let h = e.headers(headers)!
let s = h.secretData(base64SecretKey)!
let b = s.algorithm(algorithm)!
let token = b.encode

print(token!)
return token!

let header: [String: Any] = ["alg": "HS256", "typ": AuthHeaders.HeaderType]
let claims: [String: Any] = [
"nonce": nonce,
"sub": profileId,
"iss": issuer,
"aud": audience,
"iat": Int(now.timeIntervalSince1970),
"exp": Int(exp.timeIntervalSince1970)
]

let headerData = try! JSONSerialization.data(withJSONObject: header)
let claimsData = try! JSONSerialization.data(withJSONObject: claims)

let headerEncoded = base64URLEncode(headerData)
let claimsEncoded = base64URLEncode(claimsData)
let signingInput = "\(headerEncoded).\(claimsEncoded)"

let signingData = signingInput.data(using: .utf8)!
let secretData = secret.data(using: .utf8)!
let signature = hmacSHA256(data: signingData, key: secretData)

let token = "\(signingInput).\(signature)"
print(token)
return token
}

private static func base64URLEncode(_ data: Data) -> String {
return data.base64EncodedString()
.replacingOccurrences(of: "+", with: "-")
.replacingOccurrences(of: "/", with: "_")
.replacingOccurrences(of: "=", with: "")
}

private static func hmacSHA256(data: Data, key: Data) -> String {
var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
data.withUnsafeBytes { dataBytes in
key.withUnsafeBytes { keyBytes in
CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA256),
keyBytes.baseAddress, key.count,
dataBytes.baseAddress, data.count,
&digest)
}
}
return base64URLEncode(Data(digest))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


import UIKit
import SnapKit

class ChatTextMessageCell: BaseTableViewCell {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

import UIKit
import SnapKit


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


import UIKit
import CMPComapiFoundation

class ChatViewModel: NSObject {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,4 @@
#else
@import CMPComapiFoundation;
#endif
#if __has_include(<Base64/MF_Base64Additions.h>)
#import <Base64/MF_Base64Additions.h>
#elif __has_include(<MF_Base64Additions.h>)
#import <MF_Base64Additions.h>
#else
@import Base64;
#endif

#if __has_include(<JWT/JWT.h>)
#import <JWT/JWT.h>
#elif __has_include(<JWT.h>)
#import <JWT.h>
#else
@import JWT;
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


import CMPComapiFoundation

class CreateConversationViewModel {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


import UIKit
import UserNotifications

import CMPComapiFoundation

class ConversationViewModel: NSObject {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,49 @@
//

#import "CMPAuthenticationManager.h"
#import <CommonCrypto/CommonHMAC.h>

#import <JWT/JWT.h>
static NSString *base64URLEncode(NSData *data) {
NSString *base64 = [data base64EncodedStringWithOptions:0];
base64 = [base64 stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
base64 = [base64 stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
base64 = [base64 stringByReplacingOccurrencesOfString:@"=" withString:@""];
return base64;
}

@implementation CMPAuthenticationManager

+ (NSString *)generateTokenForNonce:(NSString *)nonce profileID:(NSString *)profileID issuer:(NSString *)issuer audience:(NSString *)audience secret:(NSString *)secret {
NSDate *now = [NSDate date];
NSDate *exp = [NSCalendar.currentCalendar dateByAddingUnit:NSCalendarUnitDay value:30 toDate:now options:0];

NSDictionary *headers = @{@"typ" : @"JWT"};
NSDictionary *payload = @{@"nonce" : nonce,
@"sub" : profileID,
@"iss" : issuer,
@"aud" : audience,
@"iat" : [NSNumber numberWithDouble:now.timeIntervalSince1970],
@"exp" : [NSNumber numberWithDouble:exp.timeIntervalSince1970]};


NSDictionary *header = @{@"alg": @"HS256", @"typ": @"JWT"};
NSDictionary *payload = @{@"nonce": nonce,
@"sub": profileID,
@"iss": issuer,
@"aud": audience,
@"iat": @((long long)now.timeIntervalSince1970),
@"exp": @((long long)exp.timeIntervalSince1970)};

NSData *headerData = [NSJSONSerialization dataWithJSONObject:header options:0 error:nil];
NSData *payloadData = [NSJSONSerialization dataWithJSONObject:payload options:0 error:nil];

NSString *headerEncoded = base64URLEncode(headerData);
NSString *payloadEncoded = base64URLEncode(payloadData);
NSString *signingInput = [NSString stringWithFormat:@"%@.%@", headerEncoded, payloadEncoded];

NSData *signingData = [signingInput dataUsingEncoding:NSUTF8StringEncoding];
NSData *secretData = [secret dataUsingEncoding:NSUTF8StringEncoding];
id<JWTAlgorithm> algorithm = [JWTAlgorithmFactory algorithmByName:@"HS256"];

NSString *token = [JWTBuilder encodePayload:payload].headers(headers).secretData(secretData).algorithm(algorithm).encode;
return token;

uint8_t digest[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256,
secretData.bytes, secretData.length,
signingData.bytes, signingData.length,
digest);
NSData *signatureData = [NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
NSString *signature = base64URLEncode(signatureData);

return [NSString stringWithFormat:@"%@.%@", signingInput, signature];
}

@end
17 changes: 0 additions & 17 deletions Demo/Podfile

This file was deleted.

24 changes: 0 additions & 24 deletions Demo/Podfile.lock

This file was deleted.

9 changes: 9 additions & 0 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.