| 1 | package httpsigscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/ |
| 2 | |
| 3 | import ( |
| 4 | "cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/" |
| 5 | "encoding/base64scip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/" |
| 6 | "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/" |
| 7 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 8 | "net/httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/" |
| 9 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 10 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 11 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 12 | ) |
| 13 | |
| 14 | var _ Verifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Verifier# = &verifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#{} |
| 15 | |
| 16 | type verifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier# struct { |
| 17 | headerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#header. httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Header# |
| 18 | kIdscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#kId. string |
| 19 | signaturescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#signature. string |
| 20 | createdscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#created. int64 |
| 21 | expiresscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#expires. int64 |
| 22 | headersscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#headers. []string |
| 23 | sigStringFnscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#sigStringFn. func(httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Header#, []string, int64, int64) (string, error) |
| 24 | } |
| 25 | |
| 26 | func newVerifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/newVerifier().(h httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Header#, sigStringFn func(httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Header#, []string, int64, int64) (string, error)) (*verifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#, error) { |
| 27 | scheme, s, err := getSignatureSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/getSignatureScheme().(h) |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | kId, sig, headers, created, expires, err := getSignatureComponentsscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/getSignatureComponents().(scheme, s) |
| 32 | if created != 0 { |
| 33 | //check if created is not in the future, we assume a maximum clock offset of 10 seconds |
| 34 | now := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().().Unixscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Unix().() |
| 35 | if created-now > 10 { |
| 36 | return nil, errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("created is in the future") |
| 37 | } |
| 38 | } |
| 39 | if expires != 0 { |
| 40 | //check if expires is in the past, we assume a maximum clock offset of 10 seconds |
| 41 | now := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().().Unixscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Unix().() |
| 42 | if now-expires > 10 { |
| 43 | return nil, errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("signature expired") |
| 44 | } |
| 45 | } |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | return &verifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#{ |
| 50 | headerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#header.: h, |
| 51 | kIdscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#kId.: kId, |
| 52 | signaturescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#signature.: sig, |
| 53 | createdscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#created.: created, |
| 54 | expiresscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#expires.: expires, |
| 55 | headersscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#headers.: headers, |
| 56 | sigStringFnscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#sigStringFn.: sigStringFn, |
| 57 | }, nil |
| 58 | } |
| 59 | |
| 60 | func (v *verifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#) KeyIdscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#KeyId().() string { |
| 61 | return v.kIdscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#kId. |
| 62 | } |
| 63 | |
| 64 | func (v *verifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#) Verifyscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#Verify().(pKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PublicKey#, algo Algorithmscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Algorithm#) error { |
| 65 | s, err := signerFromStringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/signerFromString().(string(algo)) |
| 66 | if err == nil { |
| 67 | return v.asymmVerifyscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#asymmVerify().(s, pKey) |
| 68 | } |
| 69 | m, err := macerFromStringscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/macerFromString().(string(algo)) |
| 70 | if err == nil { |
| 71 | return v.macVerifyscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#macVerify().(m, pKey) |
| 72 | } |
| 73 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("no crypto implementation available for %q: %s", algo, err) |
| 74 | } |
| 75 | |
| 76 | func (v *verifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#) macVerifyscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#macVerify().(m macerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/macer#, pKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PublicKey#) error { |
| 77 | key, ok := pKey.([]byte) |
| 78 | if !ok { |
| 79 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("public key for MAC verifying must be of type []byte") |
| 80 | } |
| 81 | signature, err := v.sigStringFnscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#sigStringFn.(v.headerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#header., v.headersscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#headers., v.createdscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#created., v.expiresscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#expires.) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | actualMAC, err := base64scip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/StdEncoding..DecodeStringscip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/Encoding#DecodeString().(v.signaturescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#signature.) |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | ok, err = m.Equalscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/macer#Equal.([]byte(signature), actualMAC, key) |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } else if !ok { |
| 93 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("invalid http signature") |
| 94 | } |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | func (v *verifierscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#) asymmVerifyscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#asymmVerify().(s signerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/signer#, pKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PublicKey#) error { |
| 99 | toHash, err := v.sigStringFnscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#sigStringFn.(v.headerscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#header., v.headersscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#headers., v.createdscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#created., v.expiresscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#expires.) |
| 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | signature, err := base64scip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/StdEncoding..DecodeStringscip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/Encoding#DecodeString().(v.signaturescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/verifier#signature.) |
| 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | err = s.Verifyscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/signer#Verify.(pKey, []byte(toHash), signature) |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | func getSignatureSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/getSignatureScheme().(h httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Header#) (scheme SignatureSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme#, val string, err error) { |
| 115 | s := h.Getscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Header#Get().(string(Signaturescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Signature.)) |
| 116 | sigHasAll := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(s, keyIdParameterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/keyIdParameter.) || |
| 117 | stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(s, headersParameterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/headersParameter.) || |
| 118 | stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(s, signatureParameterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/signatureParameter.) |
| 119 | a := h.Getscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Header#Get().(string(Authorizationscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Authorization.)) |
| 120 | authHasAll := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(a, keyIdParameterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/keyIdParameter.) || |
| 121 | stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(a, headersParameterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/headersParameter.) || |
| 122 | stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(a, signatureParameterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/signatureParameter.) |
| 123 | if sigHasAll && authHasAll { |
| 124 | err = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("both %q and %q have signature parameters", Signaturescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Signature., Authorizationscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Authorization.) |
| 125 | return |
| 126 | } else if !sigHasAll && !authHasAll { |
| 127 | err = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("neither %q nor %q have signature parameters", Signaturescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Signature., Authorizationscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Authorization.) |
| 128 | return |
| 129 | } else if sigHasAll { |
| 130 | val = s |
| 131 | scheme = Signaturescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Signature. |
| 132 | return |
| 133 | } else { // authHasAll |
| 134 | val = a |
| 135 | scheme = Authorizationscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/Authorization. |
| 136 | return |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func getSignatureComponentsscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/getSignatureComponents().(scheme SignatureSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme#, s string) (kId, sig string, headers []string, created int64, expires int64, err error) { |
| 141 | if as := scheme.authSchemescip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/SignatureScheme#authScheme().(); len(as) > 0 { |
| 142 | s = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimPrefix().(s, as+prefixSeparaterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/prefixSeparater.) |
| 143 | } |
| 144 | params := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.25.0 strings/Split().(s, parameterSeparaterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/parameterSeparater.) |
| 145 | for _, p := range params { |
| 146 | kv := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.SplitNscip-go gomod github.com/golang/go/src go1.25.0 strings/SplitN().(p, parameterKVSeparaterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/parameterKVSeparater., 2) |
| 147 | if len(kv) != 2 { |
| 148 | err = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("malformed http signature parameter: %v", kv) |
| 149 | return |
| 150 | } |
| 151 | k := kv[0] |
| 152 | v := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Trimscip-go gomod github.com/golang/go/src go1.25.0 strings/Trim().(kv[1], parameterValueDelimiterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/parameterValueDelimiter.) |
| 153 | switch k { |
| 154 | case keyIdParameterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/keyIdParameter.: |
| 155 | kId = v |
| 156 | case createdKeyscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/createdKey.: |
| 157 | created, err = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseInt().(v, 10, 64) |
| 158 | if err != nil { |
| 159 | return |
| 160 | } |
| 161 | case expiresKeyscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/expiresKey.: |
| 162 | expires, err = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseInt().(v, 10, 64) |
| 163 | if err != nil { |
| 164 | return |
| 165 | } |
| 166 | case algorithmParameterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/algorithmParameter.: |
| 167 | // Deprecated, ignore |
| 168 | case headersParameterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/headersParameter.: |
| 169 | headers = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.25.0 strings/Split().(v, headerParameterValueDelimscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/headerParameterValueDelim.) |
| 170 | case signatureParameterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/signatureParameter.: |
| 171 | sig = v |
| 172 | default: |
| 173 | // Ignore unrecognized parameters |
| 174 | } |
| 175 | } |
| 176 | if len(kId) == 0 { |
| 177 | err = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("missing %q parameter in http signature", keyIdParameterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/keyIdParameter.) |
| 178 | } else if len(sig) == 0 { |
| 179 | err = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("missing %q parameter in http signature", signatureParameterscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/signatureParameter.) |
| 180 | } else if len(headers) == 0 { // Optional |
| 181 | headers = defaultHeadersscip-go gomod github.com/42wim/httpsig . `github.com/42wim/httpsig`/defaultHeaders. |
| 182 | } |
| 183 | return |
| 184 | } |