Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit ae41baf

Browse files
authored
Merge pull request #42 from vulncheck-oss/tokens
🔑 token support in the SDK
2 parents 320121c + ed5e0f4 commit ae41baf

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/vulncheck-oss/sdk
33
go 1.21
44

55
require (
6+
github.com/dustin/go-humanize v1.0.1
67
github.com/getkin/kin-openapi v0.124.0
78
github.com/oapi-codegen/runtime v1.1.1
89
github.com/stretchr/testify v1.9.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvF
55
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
77
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
9+
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
810
github.com/getkin/kin-openapi v0.124.0 h1:VSFNMB9C9rTKBnQ/fpyDU8ytMTr4dWI9QovSKj9kz/M=
911
github.com/getkin/kin-openapi v0.124.0/go.mod h1:wb1aSZA/iWmorQP9KTAS/phLj/t17B5jT7+fS8ed9NM=
1012
github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q=

token.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package sdk
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/dustin/go-humanize"
7+
"time"
8+
)
9+
10+
type TokenLocation struct {
11+
City string
12+
Region string
13+
Country string
14+
TimeZone string
15+
CountryName string
16+
}
17+
18+
type TokenData struct {
19+
ID string
20+
Token string
21+
Source string
22+
Label string
23+
Icon string
24+
Ip string
25+
Agent string
26+
IsCurrent bool
27+
IsIssused bool
28+
Location TokenLocation
29+
CreatedAt string `json:"created_at"`
30+
UpdatedAt string `json:"updated_at"`
31+
}
32+
33+
type TokenResult struct {
34+
Benchmark float64 `json:"_benchmark"`
35+
Meta struct {
36+
Timestamp string `json:"timestamp"`
37+
Limit int `json:"limit"`
38+
TotalDocuments float64 `json:"total_documents"`
39+
Sort string `json:"sort"`
40+
Order string `json:"order"`
41+
Page int `json:"page"`
42+
TotalPages int `json:"total_pages"`
43+
MaxPages int `json:"max_pages"`
44+
FirstItem int `json:"first_item"`
45+
LastItem int `json:"last_item"`
46+
} `json:"_meta"`
47+
Data []TokenData
48+
}
49+
50+
type TokenResponse struct {
51+
Benchmark float64 `json:"_benchmark"`
52+
Message string `json:"message"`
53+
Success bool `json:"success"`
54+
Type string `json:"type"`
55+
Data TokenData `json:"data"`
56+
}
57+
58+
func (c *Client) GetTokens() (responseJSON *TokenResult, err error) {
59+
resp, err := c.Request("GET", "/token?limit=100")
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
defer resp.Body.Close()
65+
_ = json.NewDecoder(resp.Body).Decode(&responseJSON)
66+
return responseJSON, nil
67+
}
68+
69+
func (c *Client) CreateToken(label string) (responseJSON *TokenResponse, err error) {
70+
resp, err := c.Form("label", label).Form("icon", "i-vc-icon").Request("POST", "/token")
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
defer resp.Body.Close()
76+
_ = json.NewDecoder(resp.Body).Decode(&responseJSON)
77+
return responseJSON, nil
78+
}
79+
80+
func (c *Client) DeleteToken(ID string) (responseJSON *TokenResponse, err error) {
81+
resp, err := c.Request("DELETE", "/token/"+ID)
82+
if err != nil {
83+
return nil, err
84+
}
85+
defer resp.Body.Close()
86+
_ = json.NewDecoder(resp.Body).Decode(&responseJSON)
87+
return responseJSON, nil
88+
}
89+
90+
// GetData - Returns the data from the response
91+
func (r TokenResult) GetData() []TokenData {
92+
return r.Data
93+
}
94+
95+
// GetHumanUpdatedAt - convert 2024-09-03T23:09:14.574Z to "8 hours ago"
96+
func (t TokenData) GetHumanUpdatedAt() string {
97+
updatedAt, err := time.Parse(time.RFC3339, t.UpdatedAt)
98+
if err != nil {
99+
return "Unknown"
100+
}
101+
return humanize.Time(updatedAt)
102+
}
103+
104+
// GetLocationString - Return either Unknown or Austin, TX US
105+
func (t TokenData) GetLocationString() string {
106+
if t.Location.City == "" {
107+
return "Unknown"
108+
}
109+
return t.Location.City + ", " + t.Location.Region + " " + t.Location.Country
110+
}
111+
112+
func (t TokenData) GetSourceLabel() string {
113+
if t.Label != "" {
114+
return fmt.Sprintf("%s (%s)", t.Source, t.Label)
115+
}
116+
return t.Source
117+
}

0 commit comments

Comments
 (0)