AFSignedHTTPRequestOperationManager 4.1.0

AFSignedHTTPRequestOperationManager 4.1.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2017年1月
SwiftSwift 版本3.0
SPM支持 SPM

Adar Porat 维护。



 
依赖
AFNetworking/NSURLSession~> 3
IGDigest~> 1.1.0
 

  • 作者
  • Adar Porat

AFSignedHTTPRequestOperationManager

   

自动使用 SHA-256 哈希签名和时间戳为 AFNetworking API 请求签名。

SHA256 签名的作用是确保只有授权的 API 客户端才能发送 restful API 请求。

例如,

    [[APIClient sharedClient] POST:@"/users" parameters:@{@"some" : @"parameters"} success:^(AFHTTPRequestOperation *operation, id responseObject) {

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];

将生成一组认证参数,这些参数在服务器上可以验证。

{
  :some           => "parameters",
  :auth_timestamp => 1273231888,
  :auth_signature => "28b6bb0f242f71064916fad6ae463fe91f5adc302222dfc02c348ae1941eaf80",
  :auth_version   => "2",
  :auth_key       => "my_key"
}

要求

AFSignedHTTPRequestOperationManager 基于 AFNetworking 2.0IGDigest

安装

手动

src 文件夹复制到您的项目中。

用法

AFSignedHTTPRequestOperationManagerAFHTTPRequestOperationManager 的子类,因此可以将该类用作通常使用的 AFNetworking API 客户端。

扩展 AFSignedHTTPRequestOperationManager

#import "AFSignedHTTPRequestOperationManager.h"

@interface APIClient : AFSignedHTTPRequestOperationManager

+ (instancetype)sharedClient;

@end
#import "APIClient.h"

@implementation APIClient

+ (instancetype)sharedClient {
    static APIClient *_sharedClient = nil;
    static dispatch_once_t onceKosherPenguinToken;
    dispatch_once(&onceKosherPenguinToken, ^{
        _sharedClient = [[APIClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.example.com"]];
    });

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (self) {
        self.responseSerializer = [AFJSONResponseSerializer serializer];
        self.clientId = @"CLIENT_ID";
        self.clientSecret = @"CLIENT_SECRET";
    }

    return self;
}


@end

调用您的 API

    [[APIClient sharedClient] POST:@"/users" parameters:@{@"site_id" : @10} success:^(AFHTTPRequestOperation *operation, id responseObject) {

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];

在服务器端

Ruby - 使用 https://github.com/mloughran/signature

PHP 示例


    $client_id = 'CLIENT_ID';
    $client_secret = 'CLIENT_SECRET';

    $auth_version = $app->request->params('auth_version');
    $auth_client_id = $app->request->params('auth_client_id');
    $auth_signature = $app->request->params('auth_signature');
    $auth_timestamp = $app->request->params('auth_timestamp');

    if ($auth_version != "2") {
        throw new Exception('Incorrect client version');
    }

    if ($auth_client_id != $client_id) {
        throw new Exception('Incorrect client id');
    }

    if ($auth_timestamp <= time() - (60*60*12) || $auth_timestamp >= time() + 60*60*12) {
        throw new Exception('Incorrect auth timestamp');
    }


    // generate the auth_signature
    $params = $app->request->params();
    ksort($params);

    foreach ($params as $key => $value) {
        if (substr($key, 0, 5)=='auth_') {
            unset($params[$key]);
        }
    }

    $signatureString = '';
    foreach ($params as $key => $value) {

        if (is_array($value)) {
            foreach ($value as $item) {
                $signatureString[] = $key . '[]=' . $item;
            }
        } else {
            $signatureString[] = $key . '=' . $value;
        }
    }

    $signatureString = $app->request->getMethod() . "\n" . urldecode($app->request->getPathInfo()) . "\n" . urldecode(implode('&', $signatureString));
    $checksum = hash_hmac('sha256', $signatureString, $client_secret);

    if ($checksum!=$auth_signature) {
        throw new Exception('Incorrect auth signature ' . $signatureString);
    }