forked from mirror/jwt
gofmt
This commit is contained in:
parent
cbb3bae3aa
commit
0a26d2272f
8
jwt.go
8
jwt.go
|
@ -26,7 +26,7 @@ type Token struct {
|
|||
Valid bool
|
||||
}
|
||||
|
||||
func New(method SigningMethod)*Token {
|
||||
func New(method SigningMethod) *Token {
|
||||
return &Token{
|
||||
Header: map[string]interface{}{
|
||||
"typ": "JWT",
|
||||
|
@ -37,7 +37,7 @@ func New(method SigningMethod)*Token {
|
|||
}
|
||||
|
||||
// Get the complete, signed token
|
||||
func (t *Token) SignedString(key []byte)(string, error) {
|
||||
func (t *Token) SignedString(key []byte) (string, error) {
|
||||
var sig, sstr string
|
||||
var err error
|
||||
if sstr, err = t.SigningString(); err != nil {
|
||||
|
@ -53,7 +53,7 @@ func (t *Token) SignedString(key []byte)(string, error) {
|
|||
// most expensive part of the whole deal. Unless you
|
||||
// need this for something special, just go straight for
|
||||
// the SignedString.
|
||||
func (t *Token) SigningString()(string, error) {
|
||||
func (t *Token) SigningString() (string, error) {
|
||||
var err error
|
||||
parts := make([]string, 2)
|
||||
for i, _ := range parts {
|
||||
|
@ -150,7 +150,7 @@ func ParseFromRequest(req *http.Request, keyFunc Keyfunc) (token *Token, err err
|
|||
}
|
||||
|
||||
// Encode JWT specific base64url encoding with padding stripped
|
||||
func EncodeSegment(seg []byte)string {
|
||||
func EncodeSegment(seg []byte) string {
|
||||
return strings.TrimRight(base64.URLEncoding.EncodeToString(seg), "=")
|
||||
}
|
||||
|
||||
|
|
8
rs256.go
8
rs256.go
|
@ -2,10 +2,10 @@ package jwt
|
|||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/sha256"
|
||||
"crypto/x509"
|
||||
"crypto/rand"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
)
|
||||
|
@ -18,7 +18,7 @@ func init() {
|
|||
})
|
||||
}
|
||||
|
||||
func (m *SigningMethodRS256) Alg()string {
|
||||
func (m *SigningMethodRS256) Alg() string {
|
||||
return "RS256"
|
||||
}
|
||||
|
||||
|
@ -46,7 +46,7 @@ func (m *SigningMethodRS256) Verify(signingString, signature string, key []byte)
|
|||
return
|
||||
}
|
||||
|
||||
func (m *SigningMethodRS256) Sign(signingString string, key []byte)(sig string, err error) {
|
||||
func (m *SigningMethodRS256) Sign(signingString string, key []byte) (sig string, err error) {
|
||||
// Key
|
||||
var rsaKey *rsa.PrivateKey
|
||||
if rsaKey, err = m.parsePrivateKey(key); err == nil {
|
||||
|
@ -61,7 +61,7 @@ func (m *SigningMethodRS256) Sign(signingString string, key []byte)(sig string,
|
|||
return
|
||||
}
|
||||
|
||||
func (m *SigningMethodRS256) parsePrivateKey(key []byte)(pkey *rsa.PrivateKey, err error) {
|
||||
func (m *SigningMethodRS256) parsePrivateKey(key []byte) (pkey *rsa.PrivateKey, err error) {
|
||||
var block *pem.Block
|
||||
if block, _ = pem.Decode(key); block != nil {
|
||||
var parsedKey interface{}
|
||||
|
|
|
@ -49,7 +49,6 @@ func TestRS256Verify(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
func TestRS256Sign(t *testing.T) {
|
||||
file, _ := os.Open("test/sample_key")
|
||||
buf := new(bytes.Buffer)
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package jwt
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"crypto/hmac"
|
||||
"bytes"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
)
|
||||
|
||||
|
@ -15,7 +15,7 @@ func init() {
|
|||
})
|
||||
}
|
||||
|
||||
func (m *SigningMethodHS256) Alg()string {
|
||||
func (m *SigningMethodHS256) Alg() string {
|
||||
return "HS256"
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ func (m *SigningMethodHS256) Verify(signingString, signature string, key []byte)
|
|||
return
|
||||
}
|
||||
|
||||
func (m *SigningMethodHS256) Sign(signingString string, key []byte)(string, error) {
|
||||
func (m *SigningMethodHS256) Sign(signingString string, key []byte) (string, error) {
|
||||
hasher := hmac.New(sha256.New, key)
|
||||
hasher.Write([]byte(signingString))
|
||||
|
||||
|
|
|
@ -24,13 +24,14 @@ var sha256TestData = []struct {
|
|||
false,
|
||||
},
|
||||
}
|
||||
|
||||
// Sample data from http://tools.ietf.org/html/draft-jones-json-web-signature-04#appendix-A.1
|
||||
var sha256TestKey = []byte{
|
||||
3, 35, 53, 75, 43, 15, 165, 188, 131, 126, 6, 101, 119, 123, 166,
|
||||
143, 90, 179, 40, 230, 240, 84, 201, 40, 169, 15, 132, 178, 210, 80,
|
||||
46, 191, 211, 251, 90, 146, 210, 6, 71, 239, 150, 138, 180, 195, 119,
|
||||
98, 61, 34, 61, 46, 33, 114, 5, 46, 79, 8, 192, 205, 154, 245, 103,
|
||||
208, 128, 163 }
|
||||
208, 128, 163}
|
||||
|
||||
func TestHS256Verify(t *testing.T) {
|
||||
for _, data := range sha256TestData {
|
||||
|
|
|
@ -10,7 +10,7 @@ var signingMethods = map[string]func() SigningMethod{}
|
|||
// Signing method
|
||||
type SigningMethod interface {
|
||||
Verify(signingString, signature string, key []byte) error
|
||||
Sign(signingString string, key []byte)(string, error)
|
||||
Sign(signingString string, key []byte) (string, error)
|
||||
Alg() string
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue