Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gofeuer committed Sep 1, 2024
1 parent f726d07 commit 0d51fab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
9 changes: 3 additions & 6 deletions macaroon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/binary"
"fmt"
"reflect"
"strconv"
"strings"

macaroon "gopkg.in/macaroon.v2"
Expand Down Expand Up @@ -60,8 +59,6 @@ func UnmarshalMacaroons(macaroonBase64 string) (map[Identifier]*macaroon.Macaroo
return macaroonsMap, err
}

var byteOrder = binary.BigEndian

var (
macaroonIDSize = int(reflect.TypeFor[Identifier]().Size())
versionOffet = reflect.TypeFor[uint16]().Size()
Expand All @@ -87,7 +84,7 @@ func MarchalIdentifier(identifier Identifier) ([]byte, error) {
func UnmarshalIdentifier(identifierBytes []byte) (Identifier, error) {
if len(identifierBytes) != macaroonIDSize {
return Identifier{}, ErrUnknownVersion(-1)
} else if version := byteOrder.Uint16(identifierBytes); version != 0 {
} else if version := binary.BigEndian.Uint16(identifierBytes); version != 0 {
return Identifier{}, ErrUnknownVersion(version)
}

Expand All @@ -102,8 +99,8 @@ func UnmarshalIdentifier(identifierBytes []byte) (Identifier, error) {
return identifier, nil
}

type ErrUnknownVersion int64 //nolint:errname
type ErrUnknownVersion int //nolint:errname

func (e ErrUnknownVersion) Error() string {
return "unknown L402 version: " + strconv.FormatInt(int64(e), 10)
return fmt.Sprintf("unknown L402 version: %d", e)
}
10 changes: 6 additions & 4 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ func (p proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

const (
hexBlockSize = BlockSize * 2
expectedMatches = 3 // the header, macaroonBase64 and preimageHex
hexBlockSize = BlockSize * 2
expectedMatches = 3 // L402 (\S+):([a-f0-9]{64}) -> [the header, macaroonBase64, preimageHex]
macaroonBase64Index = 1
preimageHexIndex = 2
)

var authorizationMatcher = regexp.MustCompile(fmt.Sprintf(`L402 (\S+):([a-f0-9]{%d})`, hexBlockSize))
Expand All @@ -105,8 +107,8 @@ func getL402AuthorizationHeader(r *http.Request) (string, Hash, bool) {

for _, v := range r.Header.Values("Authorization") {
if matches := authorizationMatcher.FindStringSubmatch(v); len(matches) == expectedMatches {
macaroonBase64 := matches[1]
preimageHex := matches[2]
macaroonBase64 := matches[macaroonBase64Index]
preimageHex := matches[preimageHexIndex]

// preimageHex is guaranteed by authorizationMatcher to be 64 hexadecimal characters
hex.Decode(preimageHash[:], []byte(preimageHex)) //nolint:errcheck
Expand Down

0 comments on commit 0d51fab

Please sign in to comment.