Skip to content

Commit

Permalink
Merge pull request #32 from kmarkela/bearer
Browse files Browse the repository at this point in the history
Bearer
  • Loading branch information
kmarkela authored Nov 17, 2024
2 parents 691e84b + b3d66af commit cede56b
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 16 deletions.
Binary file modified bin/duffman_darwin_arm64
Binary file not shown.
Binary file modified bin/duffman_linux_amd64
Binary file not shown.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
)

const version = "v1.2.0-alpha"
const version = "v1.2.0"

var rootCmd = &cobra.Command{
Use: "DuffMan",
Expand Down
10 changes: 5 additions & 5 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (

func Do(req *http.Request, auth *Auth) error {

switch auth.Type {
switch strings.ToLower(auth.Type) {

case "Oauth2":
case "oauth2":
doOauth2(req, auth.Details)
case "Bearer":
doBearer(req, auth.Details["bearer"])
case "bearer":
doBearer(req, auth.Details["token"])
}

return nil
Expand Down Expand Up @@ -59,7 +59,7 @@ func doOauth2(req *http.Request, det map[string]string) error {
}
accessToken, ok := tokenResponse["access_token"].(string)
if !ok {
return fmt.Errorf("auth error. %s", err.Error())
return fmt.Errorf("auth error. No Tocken in responce")
}

doBearer(req, accessToken)
Expand Down
17 changes: 17 additions & 0 deletions internal/auth/jsonUnmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ func (at AuthType) String() string {

func (a *Auth) UnmarshalJSON(data []byte) error {

// check if alredy in custom format
var tempPrFormat struct {
Type string `json:"Type"`
Details map[string]string `json:"Details"`
}

json.Unmarshal(data, &tempPrFormat)
if tempPrFormat.Details != nil {
*a = Auth{Type: tempPrFormat.Type, Details: tempPrFormat.Details}
return nil
}

var temp struct {
Type string `json:"type"`
Oauth2 []KVT `json:"oauth2"`
Expand Down Expand Up @@ -68,6 +80,11 @@ func (a *Auth) UnmarshalJSON(data []byte) error {

}

// func (a Auth) MarshalJSON() ([]byte, error) {
// type Alias Auth
// return json.Marshal(Alias(a))
// }

func getDet(k []KVT) map[string]string {

result := map[string]string{}
Expand Down
2 changes: 1 addition & 1 deletion internal/client/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func newModel(i item, ml *model) modelEditor {
me.inputs[0].MaxHeight = len(strings.Split(req, "\n"))
me.inputs[0].SetValue(req)

vars := buildVarStr(*ml.col, *i.Req)
vars := buildVarStr(*ml.col)
me.inputs[1].CharLimit = 2 * len(vars)
me.inputs[1].MaxHeight = len(strings.Split(vars, "\n"))
me.inputs[1].SetValue(vars)
Expand Down
14 changes: 6 additions & 8 deletions internal/client/eout.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"

"github.com/kmarkela/duffman/internal/auth"
"github.com/kmarkela/duffman/internal/internalTypes"
"github.com/kmarkela/duffman/internal/pcollection"
"github.com/kmarkela/duffman/internal/req"
Expand All @@ -13,13 +12,12 @@ import (
type varOut struct {
Variables []internalTypes.KeyValue `json:"Variables,omitempty"`
Env []internalTypes.KeyValue `json:"Environment,omitempty"`
Auth *auth.Auth `json:"Auth,omitempty"`
// Auth *auth.Auth `json:"Auth,omitempty"`
}

func buildReqStr(rp pcollection.Req, env, vars []internalTypes.KeyValue) string {

r := req.DeepCopyReq(&rp)

req.ResolveVars(env, vars, r)
r.URL = req.CreateEndpoint(r.URL, r.Parameters.Get, r.Parameters.Path)
var buf bytes.Buffer
Expand All @@ -33,18 +31,18 @@ func buildReqStr(rp pcollection.Req, env, vars []internalTypes.KeyValue) string

}

func buildVarStr(col pcollection.Collection, rp pcollection.Req) string {
func buildVarStr(col pcollection.Collection) string {

var vo varOut

vo.Variables = col.Variables
vo.Env = col.Env

if rp.Auth != nil {
auth := auth.ResolveVars(col.Env, col.Variables, rp.Auth)
vo.Auth = &auth
// if rp.Auth != nil {
// auth := auth.ResolveVars(col.Env, col.Variables, rp.Auth)
// vo.Auth = &auth

}
// }

marshaled, err := json.MarshalIndent(vo, "", " ")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/pcollection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Req struct {
Body string `json:"Body,omitempty"`
ContentType string `json:"ContentType,omitempty"`
Parameters Parameters `json:"-"`
Auth *auth.Auth `json:"-"`
Auth *auth.Auth `json:"Auth"`
}

type Parameters struct {
Expand Down
7 changes: 7 additions & 0 deletions internal/req/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func ResolveVars(env, vars []internalTypes.KeyValue, req *pcollection.Req) {

allVars := append(vars, env...)

if req.Auth != nil {
// TODO: refactor
auth := auth.ResolveVars(env, vars, req.Auth)
req.Auth = &auth
}

for _, v := range allVars {

vk := fmt.Sprintf("{{%s}}", v.Key)
Expand Down Expand Up @@ -75,6 +81,7 @@ func DeepCopyReq(original *pcollection.Req) *pcollection.Req {
Post: make(map[string]string),
Path: make(map[string]string),
},
Auth: original.Auth,
}

// Copy map values for Headers
Expand Down

0 comments on commit cede56b

Please sign in to comment.