Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Schlegel committed Aug 31, 2020
1 parent 7e5f34b commit fd2aef0
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# KiwiOS Go Utilities

This repository offers various utility packages for Go artifacts in KiwiOS. It does not provide any direct executable.

## GatewayJWT

Provides the function `GetGatewayJWT(tokenURL string)` which fetches a JWT from the [Gateway Identity Service](https://bitbucket.dev.kiwigrid.com/projects/GM/repos/gateway-registry). This JWT can be used to authenticate against Kiwigrid Cloud services.

The `tokenURL` is usually retrieved via the [gateway-registry-bridge](https://bitbucket.dev.kiwigrid.com/projects/BOSS/repos/gateway-registry-bridge).
84 changes: 84 additions & 0 deletions gatewayjwt/jwt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package gatewayjwt

import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/dgrijalva/jwt-go"
)

var client = &http.Client{}
var gatewayID = ""
var gatewayKey = ""

func fetchAuthTokens() error {
cmdline, err := ioutil.ReadFile("/proc/cmdline")
if err != nil {
return err
}

args := strings.Fields(string(cmdline))
for _, v := range args {
if strings.HasPrefix(v, "kg.gateway.id=") {
gatewayID = v[14:]
} else if strings.HasPrefix(v, "kg.gateway.key=") {
gatewayKey = v[15:]
}
}

if gatewayID == "" || gatewayKey == "" {
return errors.New("Cannot read gateway id or key")
}

return nil
}

// GetGatewayJWT fetches a JWT from the IoT Identity Service used for authenticating against various Kiwigrid services.
func GetGatewayJWT(tokenURL string) (string, error) {
if gatewayID == "" || gatewayKey == "" {
err := fetchAuthTokens()
if err != nil {
return "", err
}
}

claims := &jwt.StandardClaims{
ExpiresAt: time.Now().Add(30 * time.Minute).Unix(),
Issuer: gatewayID,
Subject: gatewayID,
}

clientJwt, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(gatewayKey))

if err != nil {
return "", err
}

req, err := http.NewRequest("POST", tokenURL, nil)
req.Header.Add("Authorization", "Bearer "+clientJwt)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")

resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}

var data map[string]interface{}

if err := json.Unmarshal(body, &data); err != nil {
return "", err
}

return data["access_token"].(string), nil
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module bitbucket.dev.kiwigrid.com/BOSS/kiwios-go-utils

go 1.13

require github.com/dgrijalva/jwt-go v3.2.0+incompatible
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=

0 comments on commit fd2aef0

Please sign in to comment.