Skip to content

yyoshiki41/go-siwa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-siwa

godoc

Go library for SIWA (Sign In With Apple)

This client supports Sign in with Apple REST API to generate and validate the identity tokens.

Quick Start

  1. Generate sigend JWT from your private key
  2. Generate and Validate Tokens
    • For authorization code validation, use TokenGrantTypeAuthorizationCode.
    • For refresh token validation requests, use TokenGrantTypeRefreshToken.
package main

import (
	"context"
	"crypto/x509"
	"encoding/pem"
	"errors"
	"fmt"
	"log"
	"time"

	"github.com/yyoshiki41/go-siwa"
)

func main() {
	kid := "kid"
	teamID := "teamID"
	bundleID := "bundleID"
	privateKey := `-----BEGIN PRIVATE KEY-----
		YourPrivateKey
		-----END PRIVATE KEY-----`

	// Decode your private key
	block, _ := pem.Decode([]byte(privateKey))
	if block == nil {
		log.Fatal(errors.New("block is nil"))
	}
	pKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
	if err != nil {
		log.Fatal(err)
	}

	// 1. Generate sigend JWT from your private key
	now := time.Now()
	secret, err := siwa.NewJWTString(
		siwa.NewJWTHeader(kid),
		siwa.NewJWTPayload(teamID, bundleID,
			now.Unix(), now.Add(90*24*time.Hour).Unix()),
		pKey,
	)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%v\n", secret)

	// 2. Generate and Validate Tokens
	authorizationCode := "authorization.code"
	redirectURI := ""
	client := siwa.NewClient()
	token, err := client.TokenGrantTypeAuthorizationCode(
		context.Background(), bundleID, secret, authorizationCode, redirectURI)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%v\n", token)
}

Author

Yoshiki Nakagawa

About

Go library for SIWA (Sign In With Apple)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages