Skip to main content

JWT Authentication

Generate JWT token#

  1. Use PortOne secret key to generate JWT token

  2. Algorithm used for generating token - "HS256"

  3. Payload/claims to encode for generating token

    {
    "iss": "PORTONE",
    "sub": "SglffyyZgojEdXWL",
    "iat": 1516239022,
    "exp": 1516239122
    }
    ParameterDescriptionValues
    issIssuerDefault Value: "PORTONE"
    subSubjectPortOne Key
    iatIssued Timestamp in secTimestamp in sec
    expExpiry Timestamp in secTimestamp in sec+100
  1. Add following Headers in request as shown in below request -

    X-Portone-Client-Key: PortOne_Client_Key
    Authorization: Bearer token
  2. Sample curl request

    curl --location --request GET 'https://api.portone.cloud/api/payout/va/902000225690/balance' \
    --header 'X-Portone-Client-Key: SglffyyZgojEdXWL' \
    --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJDSEFJUEFZIiwic3ViIjoiY2hhaXBheV9jbGllbnRfaWQiLCJpYXQiOjE1MTYyMzkwMjIsImV4cCI6MTgzMDQwMDMxNn0.CJHQTY-6v5ILILamo13BhVdgK68AIH1oPwXYH4Iyffs' \
    --data-raw ''

Sample code to generate JWT token#

package main
import (
"fmt"
"time"
"github.com/golang-jwt/jwt"
)
func GenerateJWTToken1(portOneKey, portOneSecret string) string {
cur := time.Now()
expirationTime := cur.Add(100 * time.Second)
// Create the JWT claims, which includes the username and expiry time
claims := jwt.StandardClaims{
// In JWT, the expiry time is expressed as unix milliseconds
Issuer: "PORTONE",
Subject: portOneKey,
IssuedAt: cur.Unix(),
ExpiresAt: expirationTime.Unix(),
}
// Declare the token with the algorithm used for signing, and the claims
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
// Create the JWT string
tokenString, _ := token.SignedString([]byte(portOneSecret))
return tokenString
}
func main() {
portOneKey := "PORTONE_KEY"
portOneSecret := "PORTONE_SECRET"
token := GenerateJWTToken(portOneKey, portOneSecret)
fmt.Printf("The JWT Token is: %v\n", token)
}