Git Product home page Git Product logo

magic-admin-go's Introduction

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

magic-admin-go's People

Contributors

fyjen avatar justinnout avatar magic-ravi avatar smithki avatar vapopov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

magic-admin-go's Issues

Add tooling to generate DID tokens for testing

I recently started using the Magic Go SDK for the backend portion of our integration with Magic. We want to be able to test the code we have written and deployed, so it's useful to be able to generate DID tokens.

I went ahead and implemented the pseudo-code recipe for generating DID tokens (https://magic.link/docs/introduction/decentralized-id#generating-a-did-token-pseudo-code) in Go, I was wondering if this would be a useful addition to this SDK repo? I'm happy to make a PR if so!

So far I'm generating ephemeral keypairs for each test run, so naturally the magic API will not recognise these users. However, since our backend is just using the token.NewToken() constructor to validate and extract the Issuer, it doesn't matter whether the user is actually registered on the Magic side or not (we will check if we recognise the Issuer our side).

I'm also curious if you have any other thouhts to share regarding testing and if there's anything I might have missed?

Support client-side email authentication with Go

First, I love Magic! Super cool.

I'm writing a tool for my users to interact with my application via a command line interface (written in Go). Currently, I can support this by allowing them to can log into my web app using an email sent by magic.link after which they can generate a machine token to use in their terminal. This is not ideal because:

  1. I have to support two authentication methods
  2. They need to store a secret

IMO, their experience would be better if they passed their email to my CLI utility and then clicked a magic link in their email inbox, just as they would when logging into my web application. Their experience would look something like this:

$ mycli [email protected] --other-flags
please check your inbox and click the magic link in your inbox. time remaining: 4m13s

Note that I would not expect this library to be responsible for printing that message to STDERR (I'm happy to handle that myself). Therefore, my Go code could look something like this:

m := client.New("my_publishable_key", magic.NewPublicClient())
ctx, cancel := context.WithTimeout(context.Background(), time.Minute * 5)
defer cancel()
did, err := m.User.loginWithMagicLink(ctx, email)

In this example, m.User.loginWithMagicLink would block until the user clicked the magic link in their inbox.

Thanks for considering this!

P.S. Please let me know if I should move this issue to a different repository or ticketing system.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.