Skip to content

Zero dependency library for generating a Mastercard® API compliant OAuth signature in Swift

License

Notifications You must be signed in to change notification settings

lukereichold/oauth1-signer-swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Mastercard OAuth 1.0 Signature Client

Overview

Zero dependency Swift library for generating a Mastercard API compliant OAuth signature.

Usage

Prerequisites

Before using this library, you will need to set up a project and key in the Mastercard Developers Portal.

The two key pieces of information you will need are:

  • Consumer key
  • Private key matching the public key uploaded to Mastercard Developer Portal

Install

MastercardOAuth is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'MastercardOAuth'

You can then import the framework from Swift:

import MastercardOAuth

Note: Use of this library as a pod requires Xcode 10 or later.

Creating a valid OAuth Authorization header string

The method that does all the heavy lifting is OAuth.authorizationHeader(). You can call into it directly and as long as you provide the correct parameters, it will return a string that you can add into your HTTP request's Authorization header.

Example

To run the example project, first clone the repo and then pod install from the Example directory.

Example usage:

  let uri = URL(string: "https://sandbox.api.mastercard.com/service")!
  let method = "GET"
  let examplePayload: [String: String] = ["languageId": 1,
                                          "geographicId": 0]
  let payloadJSON = (try? JSONSerialization.data(withJSONObject: examplePayload, options: [])) ?? Data()
  let payloadString = String(data: payloadJSON, encoding: .utf8)
  
  let consumerKey = "<insert consumer key from developer portal>"
  let signingKey = "<initialize private key matching the consumer key>"

  let header = try? OAuth.authorizationHeader(forUri: uri, method: method, payload: payloadString, consumerKey: consumerKey, signingPrivateKey: myPrivateKey)

You can use the utility KeyProvider.swift to create a SecKey object representing your developer private key. Simply pass in the bundle path containing your Certificate.p12 to:

KeyProvider.loadPrivateKey(fromPath certificatePath: String, keyPassword: String) -> SecKey?

Author

Luke Reichold, luke@reikam.com

License

MastercardOAuth is available under the MIT license. See the LICENSE file for more info.

About

Zero dependency library for generating a Mastercard® API compliant OAuth signature in Swift

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages