From 7a06ac11bed2609484cfe0b808fd0d8fc3b1cfa9 Mon Sep 17 00:00:00 2001 From: Dave Grijalva Date: Tue, 17 Apr 2012 16:49:21 -0700 Subject: [PATCH] starting to work on it --- jwt.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ jwt_test.go | 9 +++++++ rs256.go | 10 ++++++++ signing_method.go | 25 +++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 jwt.go create mode 100644 jwt_test.go create mode 100644 rs256.go create mode 100644 signing_method.go diff --git a/jwt.go b/jwt.go new file mode 100644 index 0000000..6c29fce --- /dev/null +++ b/jwt.go @@ -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 +} diff --git a/jwt_test.go b/jwt_test.go new file mode 100644 index 0000000..16e05ae --- /dev/null +++ b/jwt_test.go @@ -0,0 +1,9 @@ +package jwt + +import ( + "testing" +) + +func TestJWT(t *testing.T) { + +} diff --git a/rs256.go b/rs256.go new file mode 100644 index 0000000..f118691 --- /dev/null +++ b/rs256.go @@ -0,0 +1,10 @@ +package jwt + +type SigningMethodRS256 struct { +} + +func init() { + RegisterSigningMethod("RS256", func() SigningMethod { + return new(SigningMethodRS256) + }) +} diff --git a/signing_method.go b/signing_method.go new file mode 100644 index 0000000..7b0c3d0 --- /dev/null +++ b/signing_method.go @@ -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 +} \ No newline at end of file