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

Commit

Permalink
Merge pull request #42 from vulncheck-oss/tokens
Browse files Browse the repository at this point in the history
🔑 token support in the SDK
  • Loading branch information
acidjazz authored Sep 18, 2024
2 parents 320121c + ed5e0f4 commit ae41baf
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/vulncheck-oss/sdk
go 1.21

require (
github.com/dustin/go-humanize v1.0.1
github.com/getkin/kin-openapi v0.124.0
github.com/oapi-codegen/runtime v1.1.1
github.com/stretchr/testify v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvF
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/getkin/kin-openapi v0.124.0 h1:VSFNMB9C9rTKBnQ/fpyDU8ytMTr4dWI9QovSKj9kz/M=
github.com/getkin/kin-openapi v0.124.0/go.mod h1:wb1aSZA/iWmorQP9KTAS/phLj/t17B5jT7+fS8ed9NM=
github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q=
Expand Down
117 changes: 117 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package sdk

import (
"encoding/json"
"fmt"
"github.com/dustin/go-humanize"
"time"
)

type TokenLocation struct {
City string
Region string
Country string
TimeZone string
CountryName string
}

type TokenData struct {
ID string
Token string
Source string
Label string
Icon string
Ip string
Agent string
IsCurrent bool
IsIssused bool
Location TokenLocation
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}

type TokenResult struct {
Benchmark float64 `json:"_benchmark"`
Meta struct {
Timestamp string `json:"timestamp"`
Limit int `json:"limit"`
TotalDocuments float64 `json:"total_documents"`
Sort string `json:"sort"`
Order string `json:"order"`
Page int `json:"page"`
TotalPages int `json:"total_pages"`
MaxPages int `json:"max_pages"`
FirstItem int `json:"first_item"`
LastItem int `json:"last_item"`
} `json:"_meta"`
Data []TokenData
}

type TokenResponse struct {
Benchmark float64 `json:"_benchmark"`
Message string `json:"message"`
Success bool `json:"success"`
Type string `json:"type"`
Data TokenData `json:"data"`
}

func (c *Client) GetTokens() (responseJSON *TokenResult, err error) {
resp, err := c.Request("GET", "/token?limit=100")
if err != nil {
return nil, err
}

defer resp.Body.Close()
_ = json.NewDecoder(resp.Body).Decode(&responseJSON)
return responseJSON, nil
}

func (c *Client) CreateToken(label string) (responseJSON *TokenResponse, err error) {
resp, err := c.Form("label", label).Form("icon", "i-vc-icon").Request("POST", "/token")
if err != nil {
return nil, err
}

defer resp.Body.Close()
_ = json.NewDecoder(resp.Body).Decode(&responseJSON)
return responseJSON, nil
}

func (c *Client) DeleteToken(ID string) (responseJSON *TokenResponse, err error) {
resp, err := c.Request("DELETE", "/token/"+ID)
if err != nil {
return nil, err
}
defer resp.Body.Close()
_ = json.NewDecoder(resp.Body).Decode(&responseJSON)
return responseJSON, nil
}

// GetData - Returns the data from the response
func (r TokenResult) GetData() []TokenData {
return r.Data
}

// GetHumanUpdatedAt - convert 2024-09-03T23:09:14.574Z to "8 hours ago"
func (t TokenData) GetHumanUpdatedAt() string {
updatedAt, err := time.Parse(time.RFC3339, t.UpdatedAt)
if err != nil {
return "Unknown"
}
return humanize.Time(updatedAt)
}

// GetLocationString - Return either Unknown or Austin, TX US
func (t TokenData) GetLocationString() string {
if t.Location.City == "" {
return "Unknown"
}
return t.Location.City + ", " + t.Location.Region + " " + t.Location.Country
}

func (t TokenData) GetSourceLabel() string {
if t.Label != "" {
return fmt.Sprintf("%s (%s)", t.Source, t.Label)
}
return t.Source
}

0 comments on commit ae41baf

Please sign in to comment.