sdkgo

package module
v0.0.0-...-82c544f Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

README

How to use SDK for signing and verifying

Signing

here is an example for calculate signature

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"

	sdk "github.com/CentauriGlobal/centauri-server/sdk/go"
)

func main() {

	// Prepare the materials needed to calculate the signature
	host := "http://127.0.0.1:8001"
	path := "/billing/v3/payments/order"
	method := "POST"
	appId := "145961149415000000000"
	serialNo := "39393631313439343138"
	timestamp := 1692153442
	nonce := "7742fbb4-1eae-4265-a226-63f8db6923dc"

	// read file to get the private key
	privateKey, err := ioutil.ReadFile("./private.pem")
	if err != nil {
		fmt.Println(err)
		return
	}

	// request body in json format
	body, err := ioutil.ReadFile("./request.json")
	if err != nil {
		fmt.Println(err)
		return
	}

	// construct http request
	payload := bytes.NewReader(body)
	client := &http.Client{}
	req, err := http.NewRequest(method, host+path, payload)
	if err != nil {
		fmt.Println(err)
		return
	}

	// add http request header
	req.Header.Add("Content-Type", "application/json")

	// calculate signature and add to header
	signParam := sdk.SignParam{
		Method:     method,
		Path:       path,
		Body:       body,
		AppID:      appId,
		SerialNo:   serialNo,
		PrivateKey: privateKey,
		Timestamp:  int64(timestamp),
		Nonce:      nonce,
		AuthIdType: "MERCHANT_ID",
	}
	authorization, err := sdk.Sign(req.Context(), signParam)
	if err != nil {
		fmt.Println(err)
		return
	}
	req.Header.Add("Authorization", authorization)

	// send request
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer res.Body.Close()

	// read from response body
	body, err = ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(body))
}

Verifying

Similar to computing a signature, the key part is as follows:

verifyParam := sdk.VerifyParam{
	Signature: signature,
	Nonce:     nonce,
	Timestamp: timestamp,
	PublicKey: publicKey,
	Body:      body,
}
err := sdk.Verify(context.TODO(), verifyParam)
if err != nil {
	log.Println(err)
}

The materials required for verifying can be obtained from the request header.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sign

func Sign(ctx context.Context, param SignParam) (authorization string, err error)

Sign calculate signature for request to midas

func Verify

func Verify(ctx context.Context, param VerifyParam) (err error)

Verify verify signature from midas

Types

type SignParam

type SignParam struct {
	// http request method, eg. POST/GET
	Method string
	// request path with leading slash
	Path string
	// request body
	Body []byte
	// app id
	AppID string
	// serial_no
	SerialNo string
	// private key
	PrivateKey []byte
	// UTC timestamp in second, if you don't want to provide, we will generate for you
	Timestamp int64
	// random string, if you don't want to provide, we will generate for you
	Nonce string
	// auth id type, if you don't want to provide, we will generate for you, default is "app_id"
	AuthIdType string
}

SignParam parameters required to calculate signature

type VerifyParam

type VerifyParam struct {
	// signature
	Signature string
	// nonce str
	Nonce string
	// UTC timestamp in seconds
	Timestamp string
	// public key
	PublicKey []byte
	// request body
	Body []byte
}

VerifyParam parameters required to verify signature

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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