Skip to content

Commit

Permalink
Added BasicAuth support.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjohnsonaz committed Aug 8, 2022
1 parent 9c7976c commit 7b3af2c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
3 changes: 0 additions & 3 deletions parser/schema_parser.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package parser

import (
"fmt"
"sort"
"strings"

Expand Down Expand Up @@ -205,8 +204,6 @@ func (p *SchemaParser) createSecuritySchemes(
result[index] = scheme
index++
}
fmt.Printf("%v", result)

return result
}

Expand Down
59 changes: 59 additions & 0 deletions templates/basic_auth.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
type Authentication struct {
*BasicAuth
}

type BasicAuth struct {
Username string
Password string
}

func EncodeBasicAuth(username string, password string) string {
escapedUsername := url.QueryEscape(username)
escapedPassword := url.QueryEscape(password)
data := base64.StdEncoding.EncodeToString([]byte(
fmt.Sprintf("%v:%v", escapedUsername, escapedPassword)))
return "Basic " + data
}

func (a BasicAuth) Encode() string {
return EncodeBasicAuth(a.Username, a.Password)
}

func DecodeAuth(header string) Authentication {
authentication := Authentication{}
if strings.HasPrefix(header, "Basic ") {
basicAuth, err := DecodeBasicAuth(header)
if err == nil {
authentication.BasicAuth = basicAuth
}
}
return authentication
}

func DecodeBasicAuth(header string) (*BasicAuth, error) {
trimmed := strings.Trim(header, "Basic ")
data, err := base64.StdEncoding.DecodeString(trimmed)
if err != nil {
return nil, err
}

parts := strings.Split(string(data), ":")
if len(parts) < 2 {
return nil, errors.New("unable to parse")
}

username, err := url.QueryUnescape(parts[0])
if err != nil {
return nil, err
}

password, err := url.QueryUnescape(parts[1])
if err != nil {
return nil, err
}

return &BasicAuth{
Username: username,
Password: password,
}, nil
}
7 changes: 7 additions & 0 deletions templates/main.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package {{ .Package }}

import (
"encoding/base64"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/gin-gonic/gin"
)

{{ template "util.tmpl" . }}

{{ template "basic_auth.tmpl" . }}

{{ range .Structs }}
{{ template "component.tmpl" . }}
{{ end }}
Expand Down
7 changes: 7 additions & 0 deletions templates/route.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ func Use{{ .Name }}(router *gin.Engine, service {{ .Name }}Service) {
return
}

{{ if gt (len .Security) 0 }}
authentication := DecodeAuth(header.Authorization)
{{ else }}
authentication := DecodeAuth("")
{{ end }}

request := {{ .Name }}Request{
params,
query,
header,
body,
authentication,
}

response, err := service.{{ .Name }}(ctx, request)
Expand Down
1 change: 1 addition & 0 deletions templates/service.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type {{ .Name }}Request struct {
{{ .Name }}Query
{{ .Name }}Header
{{ .Name }}Body
Authentication
}

type {{ .Name }}Response struct {
Expand Down
2 changes: 1 addition & 1 deletion writer/schema_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package writer
import (
"bytes"
"go/format"
"html/template"
"io/fs"
"os"
"path/filepath"
"text/template"

"github.com/cardboardrobots/go-openapi/entity"
)
Expand Down

0 comments on commit 7b3af2c

Please sign in to comment.