An open API service indexing awesome lists of open source software.

https://github.com/mastercard/oauth1-signer-go

Library for generating a Mastercard API compliant OAuth signature.
https://github.com/mastercard/oauth1-signer-go

go golang mastercard oauth1 oauth1a openapi

Last synced: about 1 year ago
JSON representation

Library for generating a Mastercard API compliant OAuth signature.

Awesome Lists containing this project

README

          

# oauth1-signer-go
[![](https://developer.mastercard.com/_/_/src/global/assets/svg/mcdev-logo-dark.svg)](https://developer.mastercard.com/)

[![](https://github.com/Mastercard/oauth1-signer-go/workflows/Build%20&%20Test/badge.svg)](https://github.com/Mastercard/oauth1-signer-go/actions?query=workflow%3A%22Build+%26+Test%22)
[![](https://goreportcard.com/badge/github.com/Mastercard/oauth1-signer-go)](https://goreportcard.com/report/github.com/Mastercard/oauth1-signer-go)
[![](https://sonarcloud.io/api/project_badges/measure?project=Mastercard_oauth1-signer-go&metric=alert_status)](https://sonarcloud.io/dashboard?id=Mastercard_oauth1-signer-go)
[![](https://github.com/Mastercard/oauth1-signer-go/workflows/broken%20links%3F/badge.svg)](https://github.com/Mastercard/oauth1-signer-go/actions?query=workflow%3A%22broken+links%3F%22)
[![](https://img.shields.io/badge/godoc-reference-5272B4.svg)](https://godoc.org/github.com/Mastercard/oauth1-signer-go)
[![](https://img.shields.io/badge/license-MIT-yellow.svg)](https://github.com/Mastercard/oauth1-signer-go/blob/master/LICENSE)

## Table of Contents
- [Overview](#overview)
* [Compatibility](#compatibility)
* [References](#references)
* [Versioning and Deprecation Policy](#versioning)
- [Usage](#usage)
* [Prerequisites](#prerequisites)
* [Installation](#installation)
* [Loading the Signing Key](#loading-the-signing-key)
* [Creating the OAuth Authorization Header](#creating-the-oauth-authorization-header)
* [Signing HTTP Request](#signing-http-request)
* [Integrating with OpenAPI Generator API Client Libraries](#integrating-with-openapi-generator-api-client-libraries)

## Overview
Library for Generating a Mastercard API compliant OAuth signature.

### Compatibility
* Go 1.20+

### References
* [OAuth 1.0a specification](https://tools.ietf.org/html/rfc5849)
* [Body hash extension for non application/x-www-form-urlencoded payloads](https://tools.ietf.org/id/draft-eaton-oauth-bodyhash-00.html)

### Versioning and Deprecation Policy
* [Mastercard Versioning and Deprecation Policy](https://github.com/Mastercard/.github/blob/main/CLIENT_LIBRARY_DEPRECATION_POLICY.md)

## Usage
### Prerequisites
Before using this library, you will need to set up a project in the [Mastercard Developers Portal](https://developer.mastercard.com).

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

####
```go
import github.com/mastercard/oauth1-signer-go
```

### Loading the Signing Key

A `signingKey` can be created by calling the `utils.LoadSigningKey` function:
```go
import "github.com/mastercard/oauth1-signer-go/utils"

//…
signingKey, err := utils.LoadSigningKey(
"",
"")
//…
```

### 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.

```go
import "github.com/mastercard/oauth1-signer-go"

//…
consumerKey := ""
url, _ := url.Parse("https://sandbox.api.mastercard.com/service")
method := "POST"
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.

```go
import (
"strings"
"github.com/mastercard/oauth1-signer-go"
)

//…
payload := strings.NewReader("")
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](https://github.com/OpenAPITools/openapi-generator) generates API client libraries from [OpenAPI Specs](https://github.com/OAI/OpenAPI-Specification).
It provides generators and library templates for supporting multiple languages and frameworks.

The `github.com/mastercard/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](#go)

#### Go
##### OpenAPI Generator

Client libraries can be generated using the following command:

```shell
openapi-generator-cli generate -i openapi-spec.yaml -g go -o out
```

See also:
* [OpenAPI Generator CLI Installation](https://openapi-generator.tech/docs/installation/)
* [CONFIG OPTIONS for Go](https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/go.md)

##### Usage of the github.com/mastercard/oauth1-signer-go/interceptor

```go
import "github.com/mastercard/oauth1-signer-go/interceptor"

//…
configuration := openapi.NewConfiguration()
configuration.BasePath = "https://sandbox.api.mastercard.com"
httpClient, _ := interceptor.GetHttpClient("", "", "")
configuration.HTTPClient = httpClient
apiClient := openapi.NewAPIClient(configuration)

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