Private
Public Access
1
0

Auth: adds JWT bearer auth via /authenticate.

Works in addition to digest auth
This commit is contained in:
James Magahern
2021-07-06 22:52:33 -07:00
parent f64ffcb8cc
commit 4d51ba7dd2
8 changed files with 314 additions and 10 deletions

View File

@@ -11,8 +11,14 @@
#import "MBIMBridge.h"
#import "MBIMBridge_Private.h"
#import "MBIMBridgeOperation.h"
#import "MBIMAuthToken.h"
#import <Security/Security.h>
#import <CocoaHTTPServer/HTTPMessage.h>
@interface HTTPConnection (/* INTERNAL */)
- (BOOL)isAuthenticated;
@end
@implementation MBIMHTTPConnection {
NSMutableData *_bodyData;
@@ -31,7 +37,15 @@
- (BOOL)isPasswordProtected:(NSString *)path
{
return [[MBIMBridge sharedInstance] usesAccessControl];
if ([[MBIMBridge sharedInstance] usesAccessControl]) {
NSURL *url = [NSURL URLWithString:path];
NSString *endpointName = [url lastPathComponent];
Class operationClass = [MBIMBridgeOperation operationClassForEndpointName:endpointName];
return [operationClass requiresAuthentication];
}
return NO;
}
- (NSString *)passwordForUser:(NSString *)username
@@ -41,7 +55,23 @@
return bridge.authPassword;
}
return @"";
// nil means "user not in system"
return nil;
}
- (BOOL)isAuthenticated
{
NSString *authInfo = [request headerField:@"Authorization"];
if ([authInfo hasPrefix:@"Bearer"]) {
NSArray *bearerAuthTuple = [authInfo componentsSeparatedByString:@" "];
if ([bearerAuthTuple count] == 2) {
NSString *jwtToken = [bearerAuthTuple objectAtIndex:1];
MBIMAuthToken *authToken = [[MBIMAuthToken alloc] initWithTokenString:jwtToken];
return [authToken isValid];
}
}
return [super isAuthenticated];
}
- (BOOL)useDigestAccessAuthentication