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.
- Host: GitHub
- URL: https://github.com/mastercard/oauth1-signer-go
- Owner: Mastercard
- License: mit
- Created: 2019-09-26T06:58:30.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2024-12-17T11:24:09.000Z (over 1 year ago)
- Last Synced: 2025-01-20T09:40:28.113Z (over 1 year ago)
- Topics: go, golang, mastercard, oauth1, oauth1a, openapi
- Language: Go
- Homepage: https://developer.mastercard.com/platform/documentation/security-and-authentication/using-oauth-1a-to-access-mastercard-apis/
- Size: 58.6 KB
- Stars: 21
- Watchers: 17
- Forks: 12
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# oauth1-signer-go
[](https://developer.mastercard.com/)
[](https://github.com/Mastercard/oauth1-signer-go/actions?query=workflow%3A%22Build+%26+Test%22)
[](https://goreportcard.com/report/github.com/Mastercard/oauth1-signer-go)
[](https://sonarcloud.io/dashboard?id=Mastercard_oauth1-signer-go)
[](https://github.com/Mastercard/oauth1-signer-go/actions?query=workflow%3A%22broken+links%3F%22)
[](https://godoc.org/github.com/Mastercard/oauth1-signer-go)
[](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.
### 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)
####
```go
import github.com/mastercard/oauth1-signer-go
```
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)
//…
```
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()
//…
```