-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
257 additions
and
1 deletion.
There are no files selected for viewing
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,53 @@ | ||
package main | ||
|
||
import ( | ||
"grpc_example/jwt/mod" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
// import ( | ||
// "fmt" | ||
// "grpc_example/jwt/mod" | ||
// "time" | ||
|
||
// "github.com/golang-jwt/jwt/v5" | ||
// ) | ||
|
||
// type MyClaims struct { | ||
// Name string | ||
// Gender int | ||
// Age int | ||
// jwt.RegisteredClaims | ||
// } | ||
|
||
// func main() { | ||
// claims := MyClaims{ | ||
// Name: "Jeremy", | ||
// Gender: 1, | ||
// Age: 18, | ||
// RegisteredClaims: jwt.RegisteredClaims{ | ||
// ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour)), | ||
// IssuedAt: jwt.NewNumericDate(time.Now()), | ||
// NotBefore: jwt.NewNumericDate(time.Now()), | ||
// }, | ||
// } | ||
|
||
// hs := mod.HS{ | ||
// Key: "fwqaaq", | ||
// } | ||
|
||
// sign, err := hs.Encode(claims) | ||
// fmt.Println(sign, err) | ||
// var outClaims MyClaims | ||
// err = hs.Decode(sign, &outClaims) | ||
// fmt.Println(outClaims, err) | ||
// } | ||
|
||
func main() { | ||
http.HandleFunc("/signin", mod.Signin) | ||
http.HandleFunc("/welcome", mod.Welcome) | ||
http.HandleFunc("/refresh", mod.Refresh) | ||
|
||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} |
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,48 @@ | ||
package mod | ||
|
||
import ( | ||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
// Key struct | ||
type Key struct { | ||
Key []byte | ||
} | ||
|
||
type Vaildator interface { | ||
Encode(claims jwt.Claims) (string, error) | ||
Decode(sign string, claims jwt.Claims) error | ||
} | ||
|
||
func (k *Key) Encode(c jwt.Claims) (string, error) { | ||
// Create the JWT token | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, c) | ||
// Create a string from the token | ||
sign, err := token.SignedString(k.Key) | ||
return sign, err | ||
} | ||
|
||
func (k *Key) Decode(sign string, c jwt.Claims) error { | ||
_, err := jwt.ParseWithClaims(sign, c, func(t *jwt.Token) (interface{}, error) { | ||
return k.Key, nil | ||
}) | ||
return err | ||
} | ||
|
||
var users = map[string]string{ | ||
"user": "password", | ||
} | ||
|
||
var jwtKey = Key{ | ||
Key: []byte("fwqaaq"), | ||
} | ||
|
||
type Credentials struct { | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
} | ||
|
||
type Claims struct { | ||
Username string `json:"username"` | ||
jwt.RegisteredClaims | ||
} |
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,52 @@ | ||
package mod | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
func Refresh(w http.ResponseWriter, r *http.Request) { | ||
c, err := r.Cookie("token") | ||
if err != nil { | ||
if err == http.ErrNoCookie { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
token := c.Value | ||
claims := &Claims{} | ||
|
||
err = jwtKey.Decode(token, claims) | ||
if err != nil { | ||
if err == jwt.ErrSignatureInvalid { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// Refresh the token when old token is going to expire(in the 30s before expiration) | ||
if time.Until(claims.ExpiresAt.Time) < 30*time.Second { | ||
w.WriteHeader(http.StatusAccepted) | ||
return | ||
} | ||
|
||
expirationTime := time.Now().Add(5 * time.Minute) | ||
claims.ExpiresAt = jwt.NewNumericDate(expirationTime) | ||
tokenString, err := jwtKey.Encode(claims) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
http.SetCookie(w, &http.Cookie{ | ||
Name: "token", | ||
Value: tokenString, | ||
Expires: expirationTime, | ||
}) | ||
} |
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,50 @@ | ||
package mod | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
func Signin(w http.ResponseWriter, r *http.Request) { | ||
var creds Credentials | ||
// Decode the request body into the struct | ||
err := json.NewDecoder(r.Body).Decode(&creds) | ||
|
||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
expectedPassword, ok := users[creds.Username] | ||
if !ok || expectedPassword != creds.Password { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
claims := &Claims{ | ||
Username: creds.Username, | ||
RegisteredClaims: jwt.RegisteredClaims{ | ||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(5 * time.Minute)), | ||
IssuedAt: jwt.NewNumericDate(time.Now()), | ||
NotBefore: jwt.NewNumericDate(time.Now()), | ||
}, | ||
} | ||
|
||
tokenString, err := jwtKey.Encode(claims) | ||
|
||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
http.SetCookie(w, &http.Cookie{ | ||
Name: "token", | ||
Value: tokenString, | ||
Expires: time.Now().Add(5 * time.Minute), | ||
}) | ||
json.NewEncoder(w).Encode(map[string]string{"message": "Successfully signed in"}) | ||
|
||
} |
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 mod | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
func Welcome(w http.ResponseWriter, r *http.Request) { | ||
// Get the token from the cookie | ||
c, err := r.Cookie("token") | ||
if err != nil { | ||
if err == http.ErrNoCookie { | ||
// If the cookie is not set, return an unauthorized status | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
// Return bad request for other errors | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
token := c.Value | ||
|
||
claims := &Claims{} | ||
|
||
err = jwtKey.Decode(token, claims) | ||
|
||
if err != nil { | ||
if err == jwt.ErrSignatureInvalid { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
w.Write([]byte("Welcome " + claims.Username)) | ||
|
||
} |
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