refactoring
This commit is contained in:
parent
6cfd02e4c9
commit
542ec6ed7b
26 changed files with 1412 additions and 625 deletions
|
@ -1,7 +1,9 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
@ -19,3 +21,15 @@ func MarshalJSON(w http.ResponseWriter, i interface{}) {
|
|||
logrus.Error("error writing response")
|
||||
}
|
||||
}
|
||||
|
||||
func ConcatenateJSON(first, second []byte) ([]byte, error) {
|
||||
if !bytes.HasSuffix(first, []byte{'}'}) {
|
||||
return nil, fmt.Errorf("jws: invalid JSON %s", first)
|
||||
}
|
||||
if !bytes.HasPrefix(second, []byte{'{'}) {
|
||||
return nil, fmt.Errorf("jws: invalid JSON %s", second)
|
||||
}
|
||||
first[len(first)-1] = ','
|
||||
first = append(first, second[1:]...)
|
||||
return first, nil
|
||||
}
|
||||
|
|
23
pkg/utils/sign.go
Normal file
23
pkg/utils/sign.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"gopkg.in/square/go-jose.v2"
|
||||
)
|
||||
|
||||
func Sign(object interface{}, signer jose.Signer) (string, error) {
|
||||
payload, err := json.Marshal(object)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return SignPayload(payload, signer)
|
||||
}
|
||||
|
||||
func SignPayload(payload []byte, signer jose.Signer) (string, error) {
|
||||
result, err := signer.Sign(payload)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return result.CompactSerialize()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue