Add 'server/' from commit '800090542d91beae40bc81fc41b67ba61c47da77'
git-subtree-dir: server git-subtree-mainline:6a4054c15agit-subtree-split:800090542d
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// MBIMAuthenticateOperation.m
|
||||
// MBIMAuthenticateOperation
|
||||
//
|
||||
// Created by James Magahern on 7/6/21.
|
||||
// Copyright © 2021 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMAuthenticateOperation.h"
|
||||
#import "MBIMBridge.h"
|
||||
#import "MBIMAuthToken.h"
|
||||
|
||||
@implementation MBIMAuthenticateOperation
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"authenticate";
|
||||
}
|
||||
|
||||
+ (BOOL)requiresAuthentication
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
NSObject<HTTPResponse> *response = nil;
|
||||
|
||||
if (self.requestBodyData.length == 0) {
|
||||
self.serverCompletionBlock([[HTTPErrorResponse alloc] initWithErrorCode:400]);
|
||||
return;
|
||||
}
|
||||
|
||||
NSError *error = nil;
|
||||
NSDictionary *args = [NSJSONSerialization JSONObjectWithData:self.requestBodyData options:0 error:&error];
|
||||
if (error || args.count == 0) {
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:400];
|
||||
} else {
|
||||
do {
|
||||
NSString *username = [args objectForKey:@"username"];
|
||||
NSString *password = [args objectForKey:@"password"];
|
||||
|
||||
if (!username || !password) {
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:400];
|
||||
break;
|
||||
}
|
||||
|
||||
if (![MBIMBridge.sharedInstance.authUsername isEqualToString:username]) {
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:401];
|
||||
break;
|
||||
}
|
||||
|
||||
if (![MBIMBridge.sharedInstance.authPassword isEqualToString:password]) {
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:401];
|
||||
break;
|
||||
}
|
||||
|
||||
MBIMAuthToken *token = [[MBIMAuthToken alloc] initWithUsername:username];
|
||||
|
||||
// All systems go
|
||||
MBIMJSONDataResponse *dataResponse = [MBIMJSONDataResponse responseWithJSONObject:@{
|
||||
@"jwt" : token.jwtToken
|
||||
}];
|
||||
|
||||
// Send a cookie down so we can use httpOnly cookies
|
||||
dataResponse.httpHeaders[@"Set-Cookie"] = [NSString stringWithFormat:@"auth_token=%@", token.jwtToken];
|
||||
|
||||
response = dataResponse;
|
||||
} while (NO);
|
||||
}
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user