From 5119d0e151b453769a80f6e565c00e9e99bf57a3 Mon Sep 17 00:00:00 2001 From: Dave Grijalva Date: Fri, 6 Jul 2012 16:07:55 -0700 Subject: [PATCH] signingn --- jwt.go | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/jwt.go b/jwt.go index 8cf55c1..d9621ce 100644 --- a/jwt.go +++ b/jwt.go @@ -14,6 +14,7 @@ type Token struct { Header map[string]interface{} Claims map[string]interface{} Method SigningMethod + // This is only populated when you Parse a token Signature string Valid bool } @@ -28,16 +29,37 @@ func New(method SigningMethod)*Token { } } -func Sign(key []byte) error { - return nil +func (t *Token) SignedString(key []byte)(string, error) { + var sig, sstr string + var err error + if sstr, err = t.SigningString(); err != nil { + return "", err + } + if sig, err = t.Method.Sign(sstr, key); err != nil { + return "", err + } + return strings.Join([]string{sstr, sig}, "."), nil } -func SigningString()string { - return "" -} - -func String()string { - return "" +func (t *Token) SigningString()(string, error) { + var err error + parts := make([]string, 2) + for i, _ := range parts { + var source map[string]interface{} + if i == 0 { + source = t.Header + } else { + source = t.Claims + } + + var jsonValue []byte + if jsonValue, err = json.Marshal(source); err != nil { + return "", err + } + + parts[i] = EncodeSegment(jsonValue) + } + return strings.Join(parts, "."), nil } // Parse, validate, and return a token.