@@ -35,16 +35,54 @@ var (
3535 ErrInvalidNowFunc = errors .New ("invalid now function" )
3636)
3737
38- // Signer holds the configuration of a signer instance
39- type Signer struct {
40- sharedKey string
41- sharedSecret string
42- prefix string
43- nowFunc NowFunc
44- signBody bool
45- signMethod bool
46- signParam bool
47- signHeaders []string
38+ // New creates an instance of Signer
39+ func New (sharedKey , sharedSecret string , options ... func (* Signer )error ) (* Signer , error ) {
40+ if sharedKey == "" {
41+ return nil , ErrMissingSharedKey
42+ }
43+ if sharedSecret == "" {
44+ return nil , ErrMissingShareSecret
45+ }
46+ signer := & Signer {
47+ sharedKey : sharedKey ,
48+ sharedSecret : sharedSecret ,
49+ }
50+ for _ , o := range options {
51+ err := o (signer )
52+ if err != nil {
53+ return nil , err
54+ }
55+ }
56+ if signer .nowFunc == nil {
57+ signer .nowFunc = func () time.Time {
58+ return time .Now ()
59+ }
60+ }
61+ if signer .prefix == "" {
62+ decoded := make ([]byte , base64 .StdEncoding .DecodedLen (len (DefaultPrefix64 )))
63+ l , _ := base64 .StdEncoding .Decode (decoded , []byte (DefaultPrefix64 ))
64+ signer .prefix = string (decoded [:l ])
65+ }
66+ return signer , nil
67+ }
68+
69+ // NewWithPrefixAndNowFunc create na instance of Signer, taking prefix and nowFunc as additional parameters
70+ func NewWithPrefixAndNowFunc (sharedKey , sharedSecret , prefix string , nowFunc NowFunc ) (* Signer , error ) {
71+ return New (sharedKey , sharedSecret ,
72+ Prefix (prefix ),
73+ WithNowFunc (nowFunc ))
74+ }
75+
76+ // GetSharedKey extracts the shared key from request
77+ func GetSharedKey (request * http.Request ) (string , error ) {
78+ signature := request .Header .Get (HeaderAuthorization )
79+
80+ comps := strings .Split (signature , ";" )
81+ if len (comps ) < 4 {
82+ return "" , ErrInvalidSignature
83+ }
84+ credential := strings .TrimPrefix (comps [1 ], "Credential:" )
85+ return credential , nil
4886}
4987
5088// SignBody includes body in the signature
@@ -101,43 +139,16 @@ func Prefix(prefix string) func(*Signer) error {
101139// NowFunc is a time source
102140type NowFunc func () time.Time
103141
104-
105- // NewWithPrefixAndNowFunc create na instance of Signer, taking prefix and nowFunc as additional parameters
106- func NewWithPrefixAndNowFunc (sharedKey , sharedSecret , prefix string , nowFunc NowFunc ) (* Signer , error ) {
107- return New (sharedKey , sharedSecret ,
108- Prefix (prefix ),
109- WithNowFunc (nowFunc ))
110- }
111-
112- // New creates an instance of Signer
113- func New (sharedKey , sharedSecret string , options ... func (* Signer )error ) (* Signer , error ) {
114- if sharedKey == "" {
115- return nil , ErrMissingSharedKey
116- }
117- if sharedSecret == "" {
118- return nil , ErrMissingShareSecret
119- }
120- signer := & Signer {
121- sharedKey : sharedKey ,
122- sharedSecret : sharedSecret ,
123- }
124- for _ , o := range options {
125- err := o (signer )
126- if err != nil {
127- return nil , err
128- }
129- }
130- if signer .nowFunc == nil {
131- signer .nowFunc = func () time.Time {
132- return time .Now ()
133- }
134- }
135- if signer .prefix == "" {
136- decoded := make ([]byte , base64 .StdEncoding .DecodedLen (len (DefaultPrefix64 )))
137- l , _ := base64 .StdEncoding .Decode (decoded , []byte (DefaultPrefix64 ))
138- signer .prefix = string (decoded [:l ])
139- }
140- return signer , nil
142+ // Signer holds the configuration of a signer instance
143+ type Signer struct {
144+ sharedKey string
145+ sharedSecret string
146+ prefix string
147+ nowFunc NowFunc
148+ signBody bool
149+ signMethod bool
150+ signParam bool
151+ signHeaders []string
141152}
142153
143154// SignRequest signs a http.Request by
@@ -253,14 +264,3 @@ func hash(data []byte, key []byte) []byte {
253264 return mac .Sum (nil )
254265}
255266
256- // GetSharedKey extracts the shared key from request
257- func GetSharedKey (request * http.Request ) (string , error ) {
258- signature := request .Header .Get (HeaderAuthorization )
259-
260- comps := strings .Split (signature , ";" )
261- if len (comps ) < 4 {
262- return "" , ErrInvalidSignature
263- }
264- credential := strings .TrimPrefix (comps [1 ], "Credential:" )
265- return credential , nil
266- }
0 commit comments