Skip to content

Instantly share code, notes, and snippets.

@bgarcial
Last active June 18, 2021 12:50
Show Gist options
  • Save bgarcial/07d2a1724b029651beec924ea9dbdc26 to your computer and use it in GitHub Desktop.
Save bgarcial/07d2a1724b029651beec924ea9dbdc26 to your computer and use it in GitHub Desktop.
Azure Active Directory Authentication via JWT to Speckle v1.

When running, we are getting the json response from AAD and the JWT to validate the assertion done.

> go run geting_auth.go
{
    "success":true,
    "resource":{
        "name":"Garcia Loaiza",
        "surname":"Bernardo",
        "logins":
            [
                {"date":1616403350385},
                {"date":1616409408858},
                {"date":1616437319494},
                {"date":1616437327734},
                {"date":1616484611374},
                {"date":1620731649145},
                {"date":1622219217329},
                {"date":1622219238417},
                {"date":1623253831993},
                {"date":1623253897266},
                {"date":1623253951408},
                {"date":1623956199503}
            ],
        "role":"admin",
        "private":true,
        "verified":true,
        "archived":false,
        "_id":"my-speckle-user-id",
        "email":"bernardo.garcia.loaiza@rhdhv.com",
        "company":"friday-burgers ",
        "apitoken":"JWT SPECKLE-API-TOKEN",
        "providerProfiles":
            {
                "azure":
                    {
                        "aio":"xxxxxxxxxxxxxxxxxxxxxxxxxx",
                        "amr":"[\"pwd\"]",
                        "email":"bernardo.garcia.loaiza@rhdhv.com",
                        "idp":"https://sts.windows.net/corporate-root-tenant-id/",
                        "ipaddr":"213.127.105.122",
                        "name":"Bernardo Garcia Loaiza",
                        "oid":"my-azure-ad-user-object-id",
                        "rh":"0.AV8A4TyaTNBh_U6GvwfA5i_f4dVSvF7wootOnEFIbWBiwihfAMI.",
                        "sub":"xxxxx-xxxx",
                        "tid":"SPECKLE-AS-SERVICE-TENANT-ID",
                        "unique_name":"bernardo.garcia.loaiza@rhdhv.com",
                        "uti":"xxxx",
                        "ver":"1.0"
                    }
            },
        "createdAt":"2021-03-21T19:17:58.244Z",
        "updatedAt":"2021-06-17T18:56:39.504Z",
        "__v":12
    }
}
O.K.
JWT SPECKLE.API.TOKEN
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
// "strings"
"testing"
"github.com/stretchr/testify/assert"
"os"
)
func main(){
var t *testing.T
SpeckleAADAuth(t)
}
func SpeckleAADAuth(t *testing.T) {
speckleServerURL := os.Getenv("SPECKLE_SERVER_URL")
//speckleServerURL := "https://friday-burgers.datafusr.rhdhv.digital/api/accounts"
method := "GET"
/*
payload := strings.NewReader(`{
"email": "bernardo.garcia.loaiza@rhdhv.com",
"password": "my-password"
}`)
*/
jsonLogin := []byte(fmt.Sprintf(`{
"email":"%s",
"password": "%s"
}`, os.Getenv("SPECKLE_EMAIL"), os.Getenv("SPECKLE_PASSWORD")))
client := &http.Client {
}
req, err := http.NewRequest(method, speckleServerURL+"/api/accounts", bytes.NewBuffer(jsonLogin))
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json, text/plain, */*")
req.Header.Add("Authorization", os.Getenv("AZURE_JWT"))
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
var speckleLogin map[string]interface{}
if err := json.Unmarshal([]byte(body), &speckleLogin); err != nil {
t.Fatal("Could not unmarshal json")
}
data := speckleLogin["resource"].(map[string]interface{})["apitoken"]
if speckleToken, ok := data.(string); ok {
if assert.NotEmpty(t, speckleToken){
fmt.Println("O.K.")
}
fmt.Println(speckleToken)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment