File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -4,12 +4,13 @@ import (
44 "bufio"
55 "bytes"
66 "encoding/hex"
7- "errors"
87 "flag"
98 "fmt"
109 "io"
1110 "log"
1211 "os"
12+
13+ "github.com/SenseUnit/basic_hmac_auth/handler"
1314)
1415
1516var version = "undefined"
@@ -52,8 +53,8 @@ func run() int {
5253 }
5354
5455 if hs == "" {
55- log .Print (`secret is not specified! Please set "-secret" or "-secret-file"` +
56- ` command line options or ` + envKeySecret + ` environment variable.` )
56+ log .Print (`secret is not specified! Please set "-secret" or "-secret-file"` +
57+ ` command line options or ` + envKeySecret + ` environment variable.` )
5758 return 2
5859 }
5960
@@ -63,8 +64,13 @@ func run() int {
6364 return 3
6465 }
6566
66- log .Printf ("secret=%x\n " , secret )
67-
67+ err = (& handler.BasicHMACAuthHandler {
68+ Secret : secret ,
69+ }).Run (os .Stdin , os .Stdout )
70+ if err != nil {
71+ log .Printf ("auth handler terminated with error: %v" , err )
72+ return 1
73+ }
6874 return 0
6975}
7076
@@ -77,7 +83,7 @@ func readSecretFromFile(filename string) (string, error) {
7783
7884 rd := bufio .NewReader (f )
7985 buf , err := rd .ReadBytes ('\n' )
80- if err != nil && ! errors . Is ( err , io .EOF ) {
86+ if err != nil && err != io .EOF {
8187 return "" , fmt .Errorf ("secret file reading failed: %w" , err )
8288 }
8389
Original file line number Diff line number Diff line change 1+ package handler
2+
3+ import (
4+ "bufio"
5+ "io"
6+ "log"
7+
8+ "github.com/SenseUnit/basic_hmac_auth/proto"
9+ )
10+
11+ const (
12+ // Sufficient for anything what might come from 64KB basic auth header
13+ DefaultBufferSize = 150 * 1024
14+ )
15+
16+ type BasicHMACAuthHandler struct {
17+ Secret []byte
18+ BufferSize int
19+ }
20+
21+ func (a * BasicHMACAuthHandler ) Run (input io.Reader , output io.Writer ) error {
22+ bufSize := a .BufferSize
23+ if bufSize <= 0 {
24+ bufSize = DefaultBufferSize
25+ }
26+ rd := bufio .NewReaderSize (input , bufSize )
27+ scanner := proto .NewElasticLineScanner (rd , '\n' )
28+
29+ for scanner .Scan () {
30+ log .Printf ("line=%q" , string (scanner .Bytes ()))
31+ }
32+
33+ return scanner .Err ()
34+ }
Original file line number Diff line number Diff line change 1+ package proto
2+
3+ import "io"
4+
5+ type BytesReader interface {
6+ ReadBytes (byte ) ([]byte , error )
7+ }
8+
9+ type ElasticLineScanner struct {
10+ line []byte
11+ reader BytesReader
12+ lastErr error
13+ done bool
14+ delim byte
15+ }
16+
17+ func NewElasticLineScanner (reader BytesReader , delim byte ) * ElasticLineScanner {
18+ return & ElasticLineScanner {
19+ reader : reader ,
20+ delim : delim ,
21+ }
22+ }
23+
24+ func (els * ElasticLineScanner ) Err () error {
25+ if els .lastErr == io .EOF {
26+ return nil
27+ }
28+ return els .lastErr
29+ }
30+
31+ func (els * ElasticLineScanner ) Bytes () []byte {
32+ return els .line
33+ }
34+
35+ func (els * ElasticLineScanner ) Scan () bool {
36+ if els .done {
37+ return false
38+ }
39+
40+ data , err := els .reader .ReadBytes (els .delim )
41+ if err != nil {
42+ els .done = true
43+ els .lastErr = err
44+ if len (data ) == 0 {
45+ return false
46+ }
47+ } else {
48+ // strip delimiter if needed
49+ if len (data ) > 0 && data [len (data )- 1 ] == els .delim {
50+ data = data [:len (data )- 1 ]
51+ }
52+ }
53+ els .line = data
54+ return true
55+ }
You can’t perform that action at this time.
0 commit comments