replace zxq.co/ripple/hanayo
This commit is contained in:
22
vendor/github.com/johnniedoe/contrib/jwt/Godeps/Godeps.json
generated
vendored
Normal file
22
vendor/github.com/johnniedoe/contrib/jwt/Godeps/Godeps.json
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"ImportPath": "github.com/gin-gonic/contrib/jwt",
|
||||
"GoVersion": "go1.3",
|
||||
"Deps": [
|
||||
{
|
||||
"ImportPath": "github.com/dgrijalva/jwt-go",
|
||||
"Rev": "5ca80149b9d3f8b863af0e2bb6742e608603bd99"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/gin-gonic/gin",
|
||||
"Rev": "ac0ad2fed865d40a0adc1ac3ccaadc3acff5db4b"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/jacobsa/oglematchers",
|
||||
"Rev": "3ecefc49db07722beca986d9bb71ddd026b133f0"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/smartystreets/goconvey/convey",
|
||||
"Rev": "958443eebb772fc6c1dcc2a44cdc6a59e1b3ff0d"
|
||||
}
|
||||
]
|
||||
}
|
83
vendor/github.com/johnniedoe/contrib/jwt/README.md
generated
vendored
Normal file
83
vendor/github.com/johnniedoe/contrib/jwt/README.md
generated
vendored
Normal file
@@ -0,0 +1,83 @@
|
||||
JWT middleware for go gonic.
|
||||
|
||||
JSON Web Token (JWT) more information: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html
|
||||
|
||||
EDIT: Below is the test for [christopherL91/Go-API](https://github.com/christopherL91/Go-API)
|
||||
|
||||
```go
|
||||
package jwt_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
func createNewsUser(username, password string) *User {
|
||||
return &User{username, password}
|
||||
}
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
Convey("Should be able to login", t, func() {
|
||||
user := createNewsUser("jonas", "1234")
|
||||
jsondata, _ := json.Marshal(user)
|
||||
post_data := strings.NewReader(string(jsondata))
|
||||
req, _ := http.NewRequest("POST", "http://localhost:3000/api/login", post_data)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
client := &http.Client{}
|
||||
res, _ := client.Do(req)
|
||||
So(res.StatusCode, ShouldEqual, 200)
|
||||
|
||||
Convey("Should be able to parse body", func() {
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
defer res.Body.Close()
|
||||
So(err, ShouldBeNil)
|
||||
Convey("Should be able to get json back", func() {
|
||||
responseData := new(Response)
|
||||
err := json.Unmarshal(body, responseData)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("Should be able to be authorized", func() {
|
||||
token := responseData.Token
|
||||
req, _ := http.NewRequest("GET", "http://localhost:3000/api/auth/testAuth", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client = &http.Client{}
|
||||
res, _ := client.Do(req)
|
||||
So(res.StatusCode, ShouldEqual, 200)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
Convey("Should not be able to login with false credentials", t, func() {
|
||||
user := createNewsUser("jnwfkjnkfneknvjwenv", "wenknfkwnfknfknkfjnwkfenw")
|
||||
jsondata, _ := json.Marshal(user)
|
||||
post_data := strings.NewReader(string(jsondata))
|
||||
req, _ := http.NewRequest("POST", "http://localhost:3000/api/login", post_data)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
client := &http.Client{}
|
||||
res, _ := client.Do(req)
|
||||
So(res.StatusCode, ShouldEqual, 401)
|
||||
})
|
||||
|
||||
Convey("Should not be able to authorize with false credentials", t, func() {
|
||||
token := ""
|
||||
req, _ := http.NewRequest("GET", "http://localhost:3000/api/auth/testAuth", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
client := &http.Client{}
|
||||
res, _ := client.Do(req)
|
||||
So(res.StatusCode, ShouldEqual, 401)
|
||||
})
|
||||
}
|
||||
```
|
46
vendor/github.com/johnniedoe/contrib/jwt/example/example.go
generated
vendored
Normal file
46
vendor/github.com/johnniedoe/contrib/jwt/example/example.go
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
jwt_lib "github.com/dgrijalva/jwt-go"
|
||||
"github.com/gin-gonic/contrib/jwt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
mysupersecretpassword = "unicornsAreAwesome"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
|
||||
public := r.Group("/api")
|
||||
|
||||
public.GET("/", func(c *gin.Context) {
|
||||
// Create the token
|
||||
token := jwt_lib.New(jwt_lib.GetSigningMethod("HS256"))
|
||||
// Set some claims
|
||||
token.Claims["ID"] = "Christopher"
|
||||
token.Claims["exp"] = time.Now().Add(time.Hour * 1).Unix()
|
||||
// Sign and get the complete encoded token as a string
|
||||
tokenString, err := token.SignedString([]byte(mysupersecretpassword))
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"message": "Could not generate token"})
|
||||
}
|
||||
c.JSON(200, gin.H{"token": tokenString})
|
||||
})
|
||||
|
||||
private := r.Group("/api/private")
|
||||
private.Use(jwt.Auth(mysupersecretpassword))
|
||||
|
||||
/*
|
||||
Set this header in your request to get here.
|
||||
Authorization: Bearer `token`
|
||||
*/
|
||||
|
||||
private.GET("/", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"message": "Hello from private"})
|
||||
})
|
||||
|
||||
r.Run("localhost:8080")
|
||||
}
|
19
vendor/github.com/johnniedoe/contrib/jwt/jwt.go
generated
vendored
Normal file
19
vendor/github.com/johnniedoe/contrib/jwt/jwt.go
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
jwt_lib "github.com/dgrijalva/jwt-go"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Auth(secret string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
_, err := jwt_lib.ParseFromRequest(c.Request, func(token *jwt_lib.Token) (interface{}, error) {
|
||||
b := ([]byte(secret))
|
||||
return b, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithError(401, err)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user