Moya + AWS API Gateway (signed V4)

Created on 21 Aug 2016  路  3Comments  路  Source: Moya/Moya

Hi guys, I am trying to get this to work with V4 signed amazon requests. This is my code so far but it does not work. Anyone here who has done this before and can help out:

class AuthenticatedProvider : RxMoyaProvider<ApiDefinition> {

    static let requestClosure = { (endpoint: Endpoint<MYENDPOINT>, closure: RequestResultClosure) -> Void in

        let request = endpoint.urlRequest.mutableCopy() as! NSMutableURLRequest

        let awsEndpoint = AWSEndpoint(region: AWSRegionType.USEast1,
                                      service: AWSServiceType.APIGateway,
                                      URL: NSURL(string:"URLGOESHERE")!)


        let signer = AWSSignatureV4Signer(credentialsProvider: AuthAWSCognitoService.sharedInstance.credentialsProvider,
                                          endpoint: awsEndpoint)

        //_configuration.baseURL = _configuration.endpoint.URL;

        let task = signer.interceptRequest(request)
        task.continueWithBlock {(task: AWSTask!) -> AnyObject? in

           closure(.Success(request))
           return nil
        }

    }


    override internal init(endpointClosure: EndpointClosure = MoyaProvider.DefaultEndpointMapping,
                           requestClosure: RequestClosure = requestClosure,
                           stubClosure: StubClosure = MoyaProvider.NeverStub,
                           manager: Manager = RxMoyaProvider<ApiDefinition>.DefaultAlamofireManager(),
                           plugins: [PluginType] = [],
                           trackInflights: Bool = false) {
        super.init(endpointClosure: endpointClosure, requestClosure: requestClosure, stubClosure: stubClosure, manager: manager, plugins: plugins, trackInflights: trackInflights)
    }
}
question

Most helpful comment

So I figured it out. Here is working code:

static let requestClosure = { (endpoint: Endpoint<APIV3Authenticated>, closure: RequestResultClosure) -> Void in

    var request = endpoint.urlRequest.mutableCopy() as! NSMutableURLRequest

    let date = NSDate.aws_clockSkewFixedDate().aws_stringValue(AWSDateISO8601DateFormat2)

    request.addValue(date,forHTTPHeaderField: "X-Amz-Date")

    let credentialsProvider = AuthAWSCognitoService.sharedInstance.credentialsProvider!

    let awsEndpoint = AWSEndpoint(region: AWSRegionType.USEast1,
                                  service: AWSServiceType.APIGateway,
                                  URL: NSURL(string:"https://.......")!)


    // Refresh credentials
    credentialsProvider.credentials().continueWithBlock {(task: AWSTask!) -> AnyObject? in


        let signer = AWSSignatureV4Signer(credentialsProvider: credentialsProvider,
            endpoint: awsEndpoint)

        //_configuration.baseURL = _configuration.endpoint.URL;

        let task = signer.interceptRequest(request)
        task.continueWithBlock {(task: AWSTask!) -> AnyObject? in



            closure(.Success(request))
            return nil
        }


        return nil
    }
}

All 3 comments

To add more info: The request gets signed but I still get 403 errors.

So I figured it out. Here is working code:

static let requestClosure = { (endpoint: Endpoint<APIV3Authenticated>, closure: RequestResultClosure) -> Void in

    var request = endpoint.urlRequest.mutableCopy() as! NSMutableURLRequest

    let date = NSDate.aws_clockSkewFixedDate().aws_stringValue(AWSDateISO8601DateFormat2)

    request.addValue(date,forHTTPHeaderField: "X-Amz-Date")

    let credentialsProvider = AuthAWSCognitoService.sharedInstance.credentialsProvider!

    let awsEndpoint = AWSEndpoint(region: AWSRegionType.USEast1,
                                  service: AWSServiceType.APIGateway,
                                  URL: NSURL(string:"https://.......")!)


    // Refresh credentials
    credentialsProvider.credentials().continueWithBlock {(task: AWSTask!) -> AnyObject? in


        let signer = AWSSignatureV4Signer(credentialsProvider: credentialsProvider,
            endpoint: awsEndpoint)

        //_configuration.baseURL = _configuration.endpoint.URL;

        let task = signer.interceptRequest(request)
        task.continueWithBlock {(task: AWSTask!) -> AnyObject? in



            closure(.Success(request))
            return nil
        }


        return nil
    }
}

Awesome, glad you got it figured out! Thanks a lot for posting your solution, I'm sure it'll be of help to others. Going to close the issue, feel free to open another if anything else comes up!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iballan picture iballan  路  35Comments

rlam3 picture rlam3  路  28Comments

Matthijn picture Matthijn  路  27Comments

BasThomas picture BasThomas  路  27Comments

justinmakaila picture justinmakaila  路  26Comments