GithubHelp home page GithubHelp logo

swawsh's Introduction

Swawsh

A cross platform library for signing AWS Signature Version 4 requests written in Swift.

http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html

Ec2 Example (macOS)

let swawsh = Swawsh.sharedInstance
let authorization = swawsh.generateCredential(
    method: .GET,
    path: "/",
    endPoint: "ec2.amazonaws.com",
    queryParameters: "Action=DescribeRegions&Version=2013-10-15",
    payloadDigest: Swawsh.emptyStringHash,
    region: "us-east-1",
    service: "ec2",
    accessKeyId: accessKeyId,
    secretKey: secretAccessKey
    )
let url = URL(string: "https://ec2.amazonaws.com/?Action=DescribeRegions&Version=2013-10-15")
var request = URLRequest(url: url!)
request.httpMethod = "GET"
request.addValue(authorization!, forHTTPHeaderField: "Authorization")
request.addValue(swawsh.getDate(), forHTTPHeaderField: "x-amz-date")
request.addValue(Swawsh.emptyStringHash, forHTTPHeaderField:"x-amz-content-sha256")
request.addValue("ec2.amazonaws.com", forHTTPHeaderField: "Host")

let session = URLSession.shared
let task = session.dataTask(with: request) { data, response, error in
    let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
    print(responseString)
}
task.resume()

S3 upload example

import Cryptor

let swawsh = Swawsh.sharedInstance
let uploadData = Data()
let sha256 = Digest(using: .sha256)
let digest = sha256.update(data: uploadData)?.final()
let digestHexString = CryptoUtils.hexString(from: digest!)

let authorization = swawsh.generateCredential(
    method: .PUT,
    path: "/my.awesome.bucket/text.txt",
    endPoint: "s3.amazonaws.com",
    queryParameters: "",
    payloadDigest: digestHexString,
    region: "us-east-1",
    service: "s3",
    accessKeyId: accessKeyId,
    secretKey: secretAccessKey
    )
{

let url = URL(string: "https://s3.amazonaws.com/my.awesome.bucket/text.txt")
var request = URLRequest(url: url!)
request.addValue(authorization!, forHTTPHeaderField: "Authorization")
request.addValue(swawsh.getDate(), forHTTPHeaderField: "x-amz-date")
request.addValue(digestHexString, forHTTPHeaderField:"x-amz-content-sha256")
request.setValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
request.httpMethod = "PUT"

let session = URLSession.shared
let task = session.uploadTask(with: request, from: uploadData) { (data, response, error) in
    let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
    print(responseString)
}
task.resume()

swawsh's People

Watchers

James Cloos avatar Nick Sweet avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.