-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from linkernetworks/johnlin/jwt-api
JWT POC for API
- Loading branch information
Showing
7 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package server | ||
|
||
import ( | ||
"strings" | ||
|
||
response "github.com/linkernetworks/vortex/src/net/http" | ||
"github.com/linkernetworks/vortex/src/web" | ||
) | ||
|
||
// TODO move to entity | ||
type UserCredentials struct { | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
} | ||
|
||
func loginHandler(ctx *web.Context) { | ||
_, req, resp := ctx.ServiceProvider, ctx.Request, ctx.Response | ||
user := UserCredentials{} | ||
if err := req.ReadEntity(&user); err != nil { | ||
response.Forbidden(req.Request, resp.ResponseWriter, err) | ||
return | ||
} | ||
|
||
// TODO query mongodb to check user account and password | ||
if strings.ToLower(user.Username) != "someone" || user.Password != "p@ssword" { | ||
response.Forbidden(req.Request, resp.ResponseWriter) | ||
return | ||
} | ||
|
||
tokenString, err := generateToken(strings.ToLower(user.Username)) | ||
if err != nil { | ||
response.InternalServerError(req.Request, resp.ResponseWriter, err) | ||
return | ||
} | ||
|
||
resp.WriteEntity(response.ActionResponse{ | ||
Error: false, | ||
Message: tokenString, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package server | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
) | ||
|
||
func generateToken(userUUID string) (string, error) { | ||
token := jwt.New(jwt.SigningMethodHS256) | ||
token.Claims = jwt.MapClaims{ | ||
// issuer of the claim | ||
"exp": time.Now().Add(time.Hour * time.Duration(1)).Unix(), | ||
// issued-at time | ||
"iat": time.Now().Unix(), | ||
// the subject of this token. This is the user associated with the relevant action | ||
"sub": userUUID, | ||
} | ||
return token.SignedString([]byte(SecretKey)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package server | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGenerateToken(t *testing.T) { | ||
tokenString, err := generateToken("234243353535330") | ||
assert.NotNil(t, tokenString) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,41 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
"github.com/dgrijalva/jwt-go/request" | ||
"github.com/emicklei/go-restful" | ||
"github.com/linkernetworks/logger" | ||
) | ||
|
||
// FIXME using ldconfig go build to give a secretkey | ||
const ( | ||
SecretKey = "linkernetworks" | ||
) | ||
|
||
func globalLogging(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) { | ||
logger.Infof("%s %s", req.Request.Method, req.Request.URL) | ||
chain.ProcessFilter(req, resp) | ||
} | ||
|
||
func validateTokenMiddleware(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) { | ||
token, err := request.ParseFromRequest(req.Request, request.AuthorizationHeaderExtractor, | ||
func(token *jwt.Token) (interface{}, error) { | ||
return []byte(SecretKey), nil | ||
}) | ||
|
||
if err == nil { | ||
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { | ||
// save to requests attributes | ||
req.SetAttribute("UserID", claims["sub"]) | ||
chain.ProcessFilter(req, resp) | ||
} else { | ||
resp.WriteHeader(http.StatusUnauthorized) | ||
logger.Infof("Token is not valid") | ||
} | ||
} else { | ||
resp.WriteHeader(http.StatusUnauthorized) | ||
logger.Infof("Unauthorized access to this resource") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters