starting to work on it

This commit is contained in:
Dave Grijalva 2012-04-17 16:49:21 -07:00
parent 0dd5b32c62
commit 7a06ac11be
4 changed files with 106 additions and 0 deletions

62
jwt.go Normal file
View File

@ -0,0 +1,62 @@
package jwt
import (
"strings"
"errors"
"encoding/base64"
"encoding/json"
)
// A JWT Token
type Token struct {
Header map[string]interface{}
Claims map[string]interface{}
Method SigningMethod
Signature string
Valid bool
}
func Parse(tokenString string, keyFunc func(*Token)(string, error)) (token *Token, err error) {
parts := strings.Split(tokenString, ".")
if len(parts) == 3 {
token = new(Token)
// parse Header
var headerBytes []byte
if headerBytes, err = base64.URLEncoding.DecodeString(parts[0]); err != nil {
return
}
if err = json.Unmarshal(headerBytes, &token.Header); err != nil {
return
}
// parse Claims
var claimBytes []byte
if claimBytes, err = base64.URLEncoding.DecodeString(parts[0]); err != nil {
return
}
if err = json.Unmarshal(claimBytes, &token.Claims); err != nil {
return
}
// Lookup signature method
if method, ok := token.Header["alg"].(string); ok {
if token.Method, err = GetSigningMethod(method); err != nil {
return
}
} else {
err = errors.New("Signing method (alg) is unspecified.")
return
}
// Check expiry times
// Lookup key
} else {
err = errors.New("Token contains an invalid number of segments")
}
return
}

9
jwt_test.go Normal file
View File

@ -0,0 +1,9 @@
package jwt
import (
"testing"
)
func TestJWT(t *testing.T) {
}

10
rs256.go Normal file
View File

@ -0,0 +1,10 @@
package jwt
type SigningMethodRS256 struct {
}
func init() {
RegisterSigningMethod("RS256", func() SigningMethod {
return new(SigningMethodRS256)
})
}

25
signing_method.go Normal file
View File

@ -0,0 +1,25 @@
package jwt
import (
"fmt"
"errors"
)
var signingMethods = map[string]func() SigningMethod{}
// Signing method
type SigningMethod interface {
}
func RegisterSigningMethod(alg string, f func() SigningMethod) {
signingMethods[alg] = f
}
func GetSigningMethod(alg string)(method SigningMethod, err error) {
if methodF, ok := signingMethods[alg]; ok {
method = methodF()
} else {
err = errors.New(fmt.Sprintf("Invalid signing method (alg): %v", method))
}
return
}