Created
September 20, 2023 08:02
-
-
Save zed-wong/2bfc7351cba6493923b1eb1771b055e0 to your computer and use it in GitHub Desktop.
exinone sign up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/sha256" | |
"encoding/base64" | |
"encoding/hex" | |
"fmt" | |
"time" | |
"github.com/fox-one/mixin-sdk-go" | |
"github.com/golang-jwt/jwt" | |
"github.com/google/uuid" | |
"golang.org/x/crypto/ed25519" | |
) | |
// UuidNewV4 generates a new UUID version 4. | |
func UuidNewV4() uuid.UUID { | |
return uuid.New() | |
} | |
/* | |
* uid: User Id | |
* sid: Session Id | |
* aud: Exinone bot Id | |
* secret: PrivateKey | |
* method: HTTP Request method, e.g.: GET, POST | |
* url: URL path without hostname, e.g.: /transfers | |
* body: HTTP Request body, e.g.: {"pin": "encrypted pin token"} | |
*/ | |
func SignAuthenticationToken(uid, sid, aud, privateKey, method, uri, body string) (string, error) { | |
expire := time.Now().UTC().Add(time.Hour * 24 * 30 * 3) | |
sum := sha256.Sum256([]byte(method + uri + body)) | |
claims := jwt.MapClaims{ | |
"uid": uid, | |
"sid": sid, | |
"iat": time.Now().UTC().Unix(), | |
"exp": expire.Unix(), | |
"jti": UuidNewV4().String(), | |
"sig": hex.EncodeToString(sum[:]), | |
"scp": "FULL", | |
"aud": aud, | |
} | |
priv, err := base64.RawURLEncoding.DecodeString(privateKey) | |
if err != nil { | |
return "", err | |
} | |
// more validate the private key | |
if len(priv) != 64 { | |
return "", fmt.Errorf("Bad ed25519 private key %s", priv) | |
} | |
token := jwt.NewWithClaims(jwt.SigningMethodEdDSA, claims) | |
return token.SignedString(ed25519.PrivateKey(priv)) | |
} | |
func main() { | |
//sessionId := `196931f3-66cf-4a01-b73c-79593d87a4e2` | |
uid := "a13f4c77-5cfc-4368-a2d6-33f07037ae9e" | |
sid := "" | |
aud := "61103d28-3ac2-44a2-ae34-bd956070dab1" | |
privateKey := "" | |
pinToken := `` | |
pinn := "" | |
method := "POST" | |
uri := "/me" | |
keyauth, err := mixin.AuthFromKeystore(&mixin.Keystore{ | |
ClientID: uid, | |
SessionID: sid, | |
PrivateKey: privateKey, | |
PinToken: pinToken, | |
Scope: "FULL", | |
}) | |
if err != nil { | |
panic(err) | |
} | |
pin := keyauth.EncryptPin(pinn) | |
body := fmt.Sprintf(`{"pin":"%s"}`, pin) | |
token, err := SignAuthenticationToken(uid, sid, aud, privateKey, method, uri, body) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("Authentication token:", token) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment