oauth

package module
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2021 License: MIT Imports: 14 Imported by: 0

README

oauth1-signer-go

Table of Contents

Overview

Library for Generating a Mastercard API compliant OAuth signature.

Compatibility
  • Go 1.12+
References

Usage

Prerequisites

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

As part of this set up, you'll receive credentials for your app:

  • A consumer key (displayed on the Mastercard Developer Portal)
  • A private request signing key (matching the public certificate displayed on the Mastercard Developer Portal)
Installation
import github.com/GreenHedgehog/oauth1-signer-go
Loading the Signing Key

A signingKey can be created by calling the utils.LoadSigningKey function:

import "github.com/GreenHedgehog/oauth1-signer-go/utils"

//...
signingKey, err := utils.LoadSigningKey(
                                    "<insert PKCS#12 key file path>", 
                                    "<insert key password>")
//...
Creating the OAuth Authorization Header

The function that does all the heavy lifting is OAuth.GetAuthorizationHeader. 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 request's Authorization header.

import "github.com/GreenHedgehog/oauth1-signer-go"

//...
consumerKey := "<insert consumer key>"
url, _ := url.Parse("https://sandbox.api.mastercard.com/service")
method := "POST"
payload := "<insert payload>"
authHeader, err := oauth.GetAuthorizationHeader(url, method, payload, consumerKey, signingKey)
//...
Signing HTTP Request

Alternatively, you can use helper function for http request.

Usage briefly described below, but you can also refer to the test package for examples.

import "github.com/GreenHedgehog/oauth1-signer-go"

//...
payload := "<insert payload>"
request, _ := http.NewRequest("POST", "https://sandbox.api.mastercard.com/service", payload)
signer := &oauth.Signer{
    ConsumerKey: consumerKey,
    SigningKey:  signingKey,
}
err = signer.Sign(request)
//...
Integrating with OpenAPI Generator API Client Libraries

OpenAPI Generator generates API client libraries from OpenAPI Specs. It provides generators and library templates for supporting multiple languages and frameworks.

The github.com/GreenHedgehog/oauth1-signer-go/interceptor package provides you function for http request interception you can use when configuring your API client. This function takes care of adding the correct Authorization header before sending the request.

Generators currently supported:

Go
OpenAPI Generator

Client libraries can be generated using the following command:

java -jar openapi-generator-cli.jar generate -i openapi-spec.yaml -g go -o out

See also:

Usage of the github.com/GreenHedgehog/oauth1-signer-go/interceptor
import "github.com/GreenHedgehog/oauth1-signer-go/interceptor"

//...
configuration := openapi.NewConfiguration()
configuration.BasePath = "https://sandbox.api.mastercard.com"
httpClient, _ := interceptor.GetHttpClient("<insert consumer key>", "<insert PKCS#12 key file path>", "<insert key password>")
configuration.HTTPClient = httpClient
apiClient := openapi.NewAPIClient(configuration)

response, err = apiClient.SomeApi.doSomething()
//...

Documentation

Overview

Package oauth performs OAuth1.0a compliant signing with body hash support for non-urlencoded content types.

Index

Constants

View Source
const (
	// AuthorizationHeaderName constant is the header key name for the
	// oauth1.0a based authorization header
	AuthorizationHeaderName = "Authorization"
)

Variables

This section is empty.

Functions

func GetAuthorizationHeader

func GetAuthorizationHeader(u *url.URL, method string, payload []byte, consumerKey string, signingKey *rsa.PrivateKey) (string, error)

GetAuthorizationHeader creates a Mastercard API compliant OAuth Authorization header.

func GetAuthorizationMasterpassHeader

func GetAuthorizationMasterpassHeader(u *url.URL, method string, payload []byte, consumerKey string, signingKey *rsa.PrivateKey) (string, error)

GetAuthorizationHeader creates a Masterpass API compliant OAuth Authorization header.

Types

type Signer

type Signer struct {
	ConsumerKey string
	SigningKey  *rsa.PrivateKey
}

Signer represents the http request signer that holds the consumer key and the signing key.

func (*Signer) Sign

func (signer *Signer) Sign(req *http.Request) error

Sign signs the http request. It generates the authorization header and sets on the header of provided http request.

Directories

Path Synopsis
Package crypto handles cryptographic related operations require to generate OAuth1.0a header.
Package crypto handles cryptographic related operations require to generate OAuth1.0a header.
Package interceptor handles oauth signing of every http request before sending.
Package interceptor handles oauth signing of every http request before sending.
Package utils handles loading of signing key.
Package utils handles loading of signing key.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL