Golang SDK Integration Guide

Overview

  1. 1.
    Video
  2. 2.
  3. 3.
    Steps
  4. 4.
    Sample

Video

Coming Soon!

Requirements

  • "encoding/json"
  • "io"
  • "net/http"
  • "bytes"

Steps

  1. 1.
  2. 2.
    Sample Codes using "net/http" library
    1. 1.
      main.go
    2. 2.
      app.go
    3. 3.
  3. 3.

1. Installation

1
go get github.com/bluenumberfoundation/humanid-golang-sdk
Copied!
For the latest version of the SDK:
1
go get -u github.com/bluenumberfoundation/humanid-golang-sdk
Copied!

2. Sample Codes using "net/http" library

main.go

1
package main
2
3
import (
4
app "[PATH_TO_APP.GO]"
5
)
6
7
func main() {
8
app := &app.App{}
9
app.Initialize()
10
11
app.Run(":[PORT_NUMBER]")
12
}
Copied!

app.go

1
import (
2
"net/http"
3
"os"
4
5
_handler "[PATH_TO_YOUR_HTTP_HANDLER]"
6
7
"github.com/gorilla/handlers"
8
mux "github.com/gorilla/mux"
9
_humanID "github.com/bluenumberfoundation/humanid-golang-sdk"
10
)
11
12
type App struct {
13
Router *mux.Router
14
}
15
16
func (app *App) Initialize() {
17
app.Router = mux.NewRouter()
18
19
humanID := _humanID.New(
20
os.Getenv("SERVER_ID"),
21
os.Getenv("SERVER_SECRET"),
22
)
23
24
_handler.NewHandler(app.Router, humanID)
25
}
26
27
28
func (app *App) Run(addr string) {
29
headers := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"})
30
methods := handlers.AllowedHeaders([]string{"GET", "POST", "PUT", "DELETE"})
31
origins := handlers.AllowedOrigins([]string{"*"})
32
}
Copied!

handler.go

1
package http
2
3
import (
4
"encoding/json"
5
"net/http"
6
"fmt"
7
8
"github.com/gorilla/mux"
9
_humanID "github.com/bluenumberfoundation/humanid-golang-sdk"
10
)
11
12
type HTTPHandler struct {
13
humanID _humanID.HumanID
14
}
15
16
func RespondJSON(w http.ResponseWriter, status int, payload interface{}) {
17
response, err := json.Marshal(payload)
18
if err != nil {
19
w.WriteHeader(http.StatusInternalServerError)
20
w.Write([]byte(err.Error()))
21
return
22
}
23
w.Header().Set("Content-Type", "application/json")
24
w.WriteHeader(status)
25
w.Write([]byte(response))
26
}
27
28
func (handler HTTPHandler) Authenticate(w http.ResponseWriter, r *http.Request) {
29
tokenArr, ok := r.URL.Query()["et"]
30
if !ok {
31
code, ok := r.URL.Query()["code"]
32
if !ok {
33
redirectSuccessURL := "[YOUR_FAIL_REDIRECT_URL]"
34
http.Redirect(w, r, redirectSuccessURL, 301)
35
return
36
}
37
}
38
token := tokenArr[0]
39
40
verifyTokenResp, err := handler.humanID.VerifyToken(token)
41
if err != nil {
42
fmt.Println(err.Error())
43
return
44
}
45
46
if verifyTokenResp.Code == "OK" {
47
redirectSuccessURL := "[YOUR_SUCCCESS_REDIRECT_URL]"
48
http.Redirect(w, r, redirectSuccessURL, 301)
49
return
50
} else {
51
redirectSuccessURL := "[YOUR_FAIL_REDIRECT_URL]"
52
http.Redirect(w, r, redirectSuccessURL, 301)
53
return
54
}
55
}
56
57
func (handler HTTPHandler) RequestLoginURL(w http.ResponseWriter, r *http.Request) {
58
loginResp, err := handler.humanID.Login("ID", "")
59
if err != nil {
60
payload := AuthenticateResponse{
61
Success: false,
62
Code: "500",
63
Message: "Internal Server Error",
64
}
65
RespondJSON(w, http.StatusInternalServerError, payload)
66
return
67
}
68
if loginResp.Code == "OK" {
69
loginURL := loginResp.Data.WebLoginUrl
70
http.Redirect(w, r, loginURL, 301)
71
return
72
}
73
payload := AuthenticateResponse{
74
Success: false,
75
Code: "500",
76
Message: "Internal Server Error",
77
}
78
RespondJSON(w, http.StatusInternalServerError, payload)
79
return
80
}
81
82
83
func NewHandler(publicRouter *mux.Router, humanID _humanID.HumanID) {
84
handler := &HTTPHandler{
85
humanID: humanID,
86
}
87
88
publicRouter.HandleFunc("/authenticate", handler.Authenticate).Methods("GET")
89
publicRouter.HandleFunc("/request", handler.RequestLoginURL).Methods("GET")
90
}
Copied!
Environment Variables
These variables can be found in the Human ID Developer Console
1
SERVER_ID=[REPLACE_ME]
2
SERVER_SECRET=[REPLACE_ME]
3
CLIENT_ID=[REPLACE_ME]
4
CLIENT_SECRET=[REPLACE_ME]
Copied!

3. Contributing

Run Tests

Pre-requisites (only for testing):
  • docker
  • docker-compose
Run tests:
1
docker-compose up
Copied!

Sample

See our Github for a full sample to learn more!