-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [WIP] Add JWT Auth * [WIP] Add jwt generation command * Added jwt_key update * Added jwt_key to swagger.yml * Applied JWT auth to fn call * Added a example of JWT auth * Set NotBefore field of StandardClaims for avoid “Token used before issued” error * update readme * Fixed flag param name * Fixed README & updated dependencies * Extract jwt related functions into common package
- Loading branch information
1 parent
e1c0012
commit 4b2d82a
Showing
11 changed files
with
251 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package common | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"time" | ||
|
||
jwt "github.com/dgrijalva/jwt-go" | ||
"github.com/dgrijalva/jwt-go/request" | ||
) | ||
|
||
func AuthJwt(signingKey string, req *http.Request) error { | ||
if signingKey == "" { | ||
return nil | ||
} | ||
|
||
extractor := request.AuthorizationHeaderExtractor | ||
tokenString, err := extractor.ExtractToken(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
token, err := jwt.ParseWithClaims(tokenString, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) { | ||
return []byte(signingKey), nil | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, ok := token.Claims.(*jwt.StandardClaims); ok && token.Valid { | ||
return nil | ||
} | ||
|
||
return errors.New("Invalid token") | ||
|
||
} | ||
|
||
func GetJwt(signingKey string, expiration int) (string, error) { | ||
now := time.Now().Unix() | ||
claims := &jwt.StandardClaims{ | ||
ExpiresAt: time.Unix(now, 0).Add(time.Duration(expiration) * time.Second).Unix(), | ||
IssuedAt: now, | ||
NotBefore: time.Unix(now, 0).Add(time.Duration(-1) * time.Minute).Unix(), | ||
} | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
ss, err := token.SignedString([]byte(signingKey)) | ||
return ss, 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Quick Example for JWT Authentication | ||
|
||
This example will show you how to test and deploy a function with JWT Authentication. | ||
|
||
```sh | ||
# create your func.yaml file | ||
fn init <YOUR_DOCKERHUB_USERNAME>/<REPO NAME> | ||
|
||
# Add | ||
# jwt_key: <Your JWT signing key> | ||
# to your func.yml | ||
|
||
# build the function | ||
fn build | ||
# test it | ||
fn run | ||
# push it to Docker Hub | ||
fn push | ||
# Create a route to this function on IronFunctions | ||
fn routes create myapp /jwt | ||
|
||
|
||
``` | ||
|
||
If you are going to add jwt authentication to an existing function, | ||
you can simply add `jwt_key` to your func.yml, and update your route | ||
using fn tool update command. | ||
|
||
Now you can call your function on IronFunctions: | ||
|
||
```sh | ||
# Get token for authentication | ||
fn routes token myapp /jwt | ||
# The token expiration time is 1 hour by default. You can also specify the expiration time explicitly. | ||
# Below example set the token expiration time at 500 seconds : | ||
fn routes token myapp /jwt 500 | ||
|
||
# The response will include a token : | ||
# { | ||
# "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDgwNTcwNTEsImlhdCI6MTUwODA1MzQ1MX0.3c_xUaleCdHy_fdU9zFB50j3hqwYWgPZ-EkTXV3VWag" | ||
# } | ||
|
||
# Now, you can access your app with a token : | ||
curl -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDgwNTcwNTEsImlhdCI6MTUwODA1MzQ1MX0.3c_xUaleCdHy_fdU9zFB50j3hqwYWgPZ-EkTXV3VWag' http://localhost:8080/r/myapp/jwt | ||
|
||
# or use fn tool | ||
# This will automatically generate a token and make function call : | ||
fn routes call myapp /jwt | ||
|
||
``` | ||
|
||
__important__: Please note that enabling Jwt authentication will require you to authenticate each time you try to call your function. | ||
You won't be able to call your function without a token. | ||
|
||
## Dependencies | ||
|
||
Be sure your dependencies are in the `vendor/` directory. | ||
|
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,17 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type Person struct { | ||
Name string | ||
} | ||
|
||
func main() { | ||
p := &Person{Name: "World"} | ||
json.NewDecoder(os.Stdin).Decode(p) | ||
fmt.Printf("Hello %v!", p.Name) | ||
} |
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
Oops, something went wrong.