|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/golang-jwt/jwt/v4" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +var users = map[string]string{ |
| 13 | + "ehsan": "1234", |
| 14 | +} |
| 15 | + |
| 16 | +type loginInfo struct { |
| 17 | + Username string `json:"username"` |
| 18 | + Password string `json:"password"` |
| 19 | +} |
| 20 | + |
| 21 | +type Claims struct { |
| 22 | + Username string |
| 23 | + jwt.StandardClaims |
| 24 | +} |
| 25 | + |
| 26 | +var jwtKey = []byte("my_secret_key") |
| 27 | + |
| 28 | +func main() { |
| 29 | + http.HandleFunc("/#", login) |
| 30 | + http.HandleFunc("/welcome", welcome) |
| 31 | + |
| 32 | + err := http.ListenAndServe(":8287", nil) |
| 33 | + if err != nil { |
| 34 | + log.Println(err) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func login(w http.ResponseWriter, r *http.Request) { |
| 39 | + var login loginInfo |
| 40 | + |
| 41 | + //decode json |
| 42 | + err := json.NewDecoder(r.Body).Decode(&login) |
| 43 | + if err != nil { |
| 44 | + w.WriteHeader(http.StatusBadRequest) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + pass, ok := users[login.Username] |
| 49 | + if !ok || pass != login.Password { |
| 50 | + w.WriteHeader(http.StatusUnauthorized) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + //set expire date |
| 55 | + expTime := time.Now().Add(5 * time.Minute) |
| 56 | + |
| 57 | + //set jwt token |
| 58 | + cliams := &Claims{ |
| 59 | + Username: login.Username, |
| 60 | + StandardClaims: jwt.StandardClaims{ |
| 61 | + //expire time as a unix |
| 62 | + ExpiresAt: expTime.Unix(), |
| 63 | + }, |
| 64 | + } |
| 65 | + //get method to hash token |
| 66 | + token := jwt.NewWithClaims(jwt.SigningMethodHS256, cliams) |
| 67 | + |
| 68 | + stringToken, err := token.SignedString(jwtKey) |
| 69 | + if err != nil { |
| 70 | + w.WriteHeader(http.StatusInternalServerError) |
| 71 | + return |
| 72 | + } |
| 73 | + //set cookie for set token |
| 74 | + http.SetCookie(w, &http.Cookie{ |
| 75 | + Name: "JWTToken", |
| 76 | + Expires: expTime, |
| 77 | + Value: stringToken, |
| 78 | + HttpOnly: true, |
| 79 | + }) |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +func welcome(w http.ResponseWriter, r *http.Request) { |
| 84 | + //get jwt token form token |
| 85 | + c, err := r.Cookie("JWTToken") |
| 86 | + if err != nil { |
| 87 | + fmt.Println(err) |
| 88 | + w.WriteHeader(http.StatusUnauthorized) |
| 89 | + return |
| 90 | + } |
| 91 | + //validate user |
| 92 | + tokenSting := c.Value |
| 93 | + //pars token |
| 94 | + climes := &Claims{} |
| 95 | + token, err := jwt.ParseWithClaims(tokenSting, climes, func(token *jwt.Token) (interface{}, error) { |
| 96 | + return jwtKey, nil |
| 97 | + }) |
| 98 | + if !token.Valid { |
| 99 | + w.WriteHeader(http.StatusUnauthorized) |
| 100 | + return |
| 101 | + } |
| 102 | + if err != nil { |
| 103 | + if err == jwt.ErrSignatureInvalid { |
| 104 | + w.WriteHeader(http.StatusUnauthorized) |
| 105 | + return |
| 106 | + } |
| 107 | + w.WriteHeader(http.StatusBadRequest) |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + fmt.Fprintln(w, "welcome dear") |
| 113 | +} |
0 commit comments