package
1.0.17
Repository: https://github.com/parinyapt/golang_utils.git
Documentation: pkg.go.dev

# README

PTGU OAuth Apple

Import

import (
	PTGUoauth "github.com/parinyapt/golang_utils/oauth/apple/v1"
)

Example

Config OAuth

var privkey = `
-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----`

appleOAuth := PTGUoauth.NewAppleOAuth(&PTGUoauth.AppleOAuthConfig{
  ClientID:    "com.prinpt.devx",
  RedirectURL: "https://apple.prinpt.com/callback",
  TeamID:      "XXXXXXXXXX",
  KeyID:       "XXXXXXXXXX",
  PrivateKey:  privkey,
})

Generate Login URL

loginURL := appleOAuth.GenerateOAuthURL(PTGUoauth.OptionAppleGenerateOAuthURL{
  ResponseType: []string{"code", "id_token"},
  ResponseMode: "form_post",
  Scope:        []string{"name", "email"},
  State:        "STATE",
})

Generate Client Secret

clientSecret, err := appleOAuth.GenerateClientSecret(5 * time.Minute)
if err != nil {
	panic(err)
}
fmt.Println(clientSecret)

Get IDToken Info from JWT

tokenString := "eyJra...96sZg"
data, err := PTGUoauth.GetIDTokenInfo(tokenString)
if err != nil {
  panic(err)
}
fmt.Println(data.Audience)
if data.Email != nil {
  fmt.Println(*data.Email)
}

Get IDToken Info from JWT with validate Public Key

tokenString := "eyJra...96sZg"
data, ispass , err := appleOAuth.GetIDTokenInfoWithPublicKeyValidation(tokenString, PTGUoauth.OptionAppleGetIDTokenInfoWithPublicKeyValidation{
  NotIssuedBeforeTime: time.Now().Add(-1 * time.Hour), // optional
  ExpiresAfterIssuedIn: 10 * time.Hour, // optional
})
if err != nil {
  panic(err)
}
fmt.Println(ispass)
fmt.Println(data.Audience)
if data.Email != nil {
  fmt.Println(*data.Email)
}

Get Apple Public Key

pubKey, err := PTGUoauth.GetApplePublicKey("XXXXXXX")
if err != nil {
  panic(err)
}
fmt.Println(pubKey.N)
fmt.Println(pubKey.E)

Validate Authorization Code and Get Access Token / ID Token / Refresh Token

code := "c7...lABoQ"
data, err := appleOAuth.ValidateAuthorizationCode(code, PTGUoauth.PlatformWeb) // PTGUoauth.PlatformWeb or PTGUoauth.PlatformApp
if err != nil {
  panic(err)
}
fmt.Println(data.AccessToken)
fmt.Println(data.RefreshToken)
fmt.Println(data.TokenType)
fmt.Println(data.ExpiresIn)
fmt.Println(data.IDToken)

Validate Refresh Token and Get Access Token / ID Token

refreshToken := "rca7...lABoQ"
data, err := appleOAuth.ValidateRefreshToken(refreshToken)
if err != nil {
  panic(err)
}
fmt.Println(data.IDToken)
fmt.Println(data.AccessToken)
fmt.Println(data.ExpiresIn)
fmt.Println(data.TokenType)

Revoke Token by Access Token or Refresh Token

token := "rca7...lABoQ"
err := appleOAuth.RevokeToken(token, PTGUoauth.TypeRefreshToken) // PTGUoauth.TypeAccessToken or PTGUoauth.TypeRefreshToken
if err != nil {
  panic(err)
}

REF

# Functions

!GetApplePrivateKeyFromFile.
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

URL for Apple OAuth.
No description provided by the author
No description provided by the author
URL for fetch Apple's public key for verifying token signature.
URL for revoke tokens.
!RevokeToken.
!RevokeToken.
URL for generate and validate tokens.

# Structs

!GetIDTokenInfo.
No description provided by the author
!ValidateAuthorizationCode.
!ValidateRefreshToken.
!GenerateOAuthURL.
!GetIDTokenInfoWithPublicKeyValidation.
!GetApplePublicKey.

# Interfaces

No description provided by the author