Skip to content

Magic admin Go SDK makes it easy to leverage Decentralized ID tokens to protect routes and restricted resources for your application.

License

Notifications You must be signed in to change notification settings

magiclabs/magic-admin-go

Repository files navigation

Magic Admin Golang SDK

The Magic Admin Golang SDK provides convenient ways for developers to interact with Magic API endpoints and an array of utilities to handle DID Token.

Table of Contents

Documentation

See the Magic doc!

Installation

The SDK requires Golang 1.13+ and Go Modules. To make sure your project is using Go Modules, you can look for go.mod file in your project's root directory. If it exits, then you are already using the Go Modules. If not, you can follow this guide to migrate to Go Modules.

Simply reference magic-admin-go in a Go program with an import of the SDK:

import (
    ...
    "github.com/magiclabs/magic-admin-go"
    ...
)

Run any of the normal go commands (ex: build/install). The Go toolchain will take care of fetching the SDK automatically.

Alternatively, you can explicitly go get the package into a project:

go get github.com/magiclabs/magic-admin-go

Command line utility

Command line utility is created for testing purposes and can be used for decoding and validating DID tokens. It also provides functionality to retrieve user info.

You can simply install it by the command:

go install github.com/magiclabs/magic-admin-go/cmd/magic-cli

Current available command supported:

$ magic-cli -h
NAME:
   magic-cli - command line utility to make requests to api and validate tokens

USAGE:
   magic-cli [global options] command [command options] [arguments...]

COMMANDS:
   token, t   magic-cli token [decode|validate] --did <DID token> [--clientId <Magic Client ID>]
   user, u    magic-cli -s <secret> user --did <DID token>
   help, h    Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --secret value, -s value  Secret token which will be used for making request to backend api [$MAGIC_API_SECRET_KEY]
   --help, -h                show help (default: false)

Quick Start

Before you start, you will need an API secret key. You can get one from the Magic Dashboard. Once you have the API secret key, you can instantiate a Magic object.

Sample code to retrieve user info by a DID token:

package main

import (
    "log"
    "fmt"

    "github.com/magiclabs/magic-admin-go"
    "github.com/magiclabs/magic-admin-go/client"
)

func main() {
    m, err := client.New("<YOUR_API_SECRET_KEY>", magic.NewDefaultClient())
    userInfo, err := m.User.GetMetadataByToken("<DID_TOKEN>")
    if err != nil {
        log.Fatalf("Error: %s", err.Error())
    }

    fmt.Println(userInfo)
}

Sample code to validate a DID token and retrieve the claim and proof from the token:

package main

import (
    "log"
    "fmt"

	"github.com/magiclabs/magic-admin-go/client"
    "github.com/magiclabs/magic-admin-go/token"
)

func main() {

	c, err := client.New("<YOUR_API_SECRET_KEY>", magic.NewDefaultClient())

	if err != nil {
		log.Fatalf("Unable to initialize client: %s", err.Error())
	}
	
	tk, err := token.NewToken("<DID_TOKEN>")
    if err != nil {
        log.Fatalf("DID token is malformed: %s", err.Error())
    }
    
    if err := tk.Validate(c.ClientInfo.ClientId); err != nil {
        log.Fatalf("DID token is invalid: %v", err)
    }

    fmt.Println(tk.GetClaim())
    fmt.Println(tk.GetProof())
}

Configure Network Strategy

The NewClientWithRetry method creates a client with retries, retryWait, timeout options. NewClientWithRetry returns a *resty.Client instance which can be used with the Magic client.

cl := magic.NewClientWithRetry(5, time.Second, 10 * time.Second)
m := client.New("<YOUR_API_SECRET_KEY>", cl)

Development

We would love to have you contribute to the SDK. To get started, you will need to clone this repository and fetch the dependencies.

To run the existing tests:

make test

To build and install magic-cli utility tool, you can run:

make install

To build magic-cli utility tool separately as a binary, you can run:

make build

Please also see our CONTRIBUTING guide for more information.

Changelog

See Changelog

License

See License

About

Magic admin Go SDK makes it easy to leverage Decentralized ID tokens to protect routes and restricted resources for your application.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 5