Coder Social home page Coder Social logo

gqadonis / magic-admin-go Goto Github PK

View Code? Open in Web Editor NEW

This project forked from magiclabs/magic-admin-go

0.0 0.0 0.0 46 KB

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

Home Page: https://docs.magic.link/admin-sdk/go

License: MIT License

Go 98.54% Makefile 1.46%

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>
   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 := 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/token"
)

func main() {
    tk, err := token.NewToken("<DID_TOKEN>")
    if err != nil {
        log.Fatalf("DID token is malformed: %s", err.Error())
    }
    
    if err := tk.Validate(); 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

vapopov avatar fyjen avatar smithki avatar

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.