Skip to content

Commit

Permalink
fix: improve code a bit
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Feb 21, 2020
1 parent 1736e27 commit 5372c37
Showing 1 changed file with 18 additions and 36 deletions.
54 changes: 18 additions & 36 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
)

var (
authorizations authorizationSlice
bucket string
address string
auths stringSlice
bucket string
listen string
)

func main() {
flag.Var(&authorizations, "authorize", "usernames:passwords to be used to authenticate (e.g.: carlos:asd123)")
flag.Var(&auths, "authorize", "user/passwords that can authenticate in the user:pwd format (e.g.: carlos:asd123)")
flag.StringVar(&bucket, "bucket", "", "bucket name (e.g.: s3://foo)")
flag.StringVar(&address, "addr", "127.0.0.1:8080", "address to listen to (e.g. 127.0.0.1:9090)")
flag.StringVar(&listen, "listen", "127.0.0.1:8080", "address to listen to (e.g. 127.0.0.1:9090)")
flag.Parse()

ctx := context.Background()
Expand All @@ -42,8 +42,9 @@ func main() {
var path = strings.Replace(r.URL.EscapedPath(), "/", "", 1)
log.Println(path)

if !authorize(r) {
log.Println("unauthorized")
user, pwd, ok := r.BasicAuth()
if !(ok && isAuthorized(user+":"+pwd)) {
log.Println("unauthorized:", user)
return httperr.Wrap(fmt.Errorf("missing/invalid authorization"), http.StatusUnauthorized)
}

Expand All @@ -58,43 +59,24 @@ func main() {
return nil
}))

log.Println("listening on", address)
http.ListenAndServe(address, handler)
log.Println("listening on", listen)
http.ListenAndServe(listen, handler)
}

type authorization struct {
Username, Password string
}

type authorizationSlice []authorization
type stringSlice []string

func (i *authorizationSlice) String() string {
var strs []string
for _, a := range *i {
strs = append(strs, a.Username)
}
return "[" + strings.Join(strs, ", ") + "]"
func (i *stringSlice) String() string {
return "[" + strings.Join(*i, ", ") + "]"
}

func (i *authorizationSlice) Set(value string) error {
var parts = strings.Split(value, ":")
if len(parts) != 2 {
return fmt.Errorf("must be in the username:password format")
}
*i = append(*i, authorization{
parts[0],
parts[1],
})
func (i *stringSlice) Set(value string) error {
*i = append(*i, value)
return nil
}

func authorize(r *http.Request) bool {
user, pwd, ok := r.BasicAuth()
if !ok {
return false
}
for _, auth := range authorizations {
if auth.Username == user && auth.Password == pwd {
func isAuthorized(input string) bool {
for _, auth := range auths {
if input == auth {
return true
}
}
Expand Down

0 comments on commit 5372c37

Please sign in to comment.