forked from MicrosoftStudentChapter/Linky
-
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.
Merge pull request #6 from DarkCoder26/Auth
Jwt token and register added in auth
- Loading branch information
Showing
7 changed files
with
139 additions
and
162 deletions.
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
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,105 @@ | ||
package auth | ||
|
||
import ( | ||
// "fmt" | ||
"encoding/json" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
) | ||
|
||
var jwtKey = []byte(os.Getenv("JWT_SECRET")) | ||
var users = map[string]string{} | ||
|
||
type Claims struct { | ||
Username string "json:username" | ||
jwt.StandardClaims | ||
} | ||
|
||
func GenerateJWT(w http.ResponseWriter, r *http.Request) { | ||
username := r.URL.Query().Get("username") | ||
if username == "" { | ||
http.Error(w, "Username is required", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
expirationTime := time.Now().Add(30 * time.Minute) | ||
claims := &Claims{ | ||
Username: username, | ||
StandardClaims: jwt.StandardClaims{ | ||
ExpiresAt: expirationTime.Unix(), | ||
IssuedAt: time.Now().Unix(), | ||
Issuer: "Linky", | ||
}, | ||
} | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
tokenString, err := token.SignedString(jwtKey) | ||
if err != nil { | ||
http.Error(w, "Could not generate token", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(`{"token": "` + tokenString + `"}`)) | ||
// return tokenString | ||
} | ||
|
||
func ValidateJWT(w http.ResponseWriter, r *http.Request) { | ||
tokenString := r.URL.Query().Get("token") | ||
claims := &Claims{} | ||
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) { | ||
return jwtKey, nil | ||
}) | ||
|
||
if err != nil { | ||
if err == jwt.ErrSignatureInvalid { | ||
http.Error(w, "Invalid token signature", http.StatusUnauthorized) | ||
return | ||
} | ||
http.Error(w, "Invalid token", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if !token.Valid { | ||
http.Error(w, "Invalid token", http.StatusUnauthorized) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(map[string]interface{}{ | ||
"username": claims.Username, | ||
"id": claims.Id, | ||
"issuer": claims.Issuer, | ||
"expiresAt": claims.ExpiresAt, | ||
"issuedAt": claims.IssuedAt, | ||
}) | ||
|
||
// return claims, nil | ||
} | ||
|
||
func Register(w http.ResponseWriter, r *http.Request) { | ||
username := r.URL.Query().Get("username") | ||
password := r.URL.Query().Get("password") | ||
|
||
if username == "" || password == "" { | ||
http.Error(w, "Username and password are required", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if _, exists := users[username]; exists { | ||
http.Error(w, "User already exists", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
users[username] = password | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
json.NewEncoder(w).Encode(map[string]string{"message": "User registered successfully"}) | ||
} | ||
|
||
func ShowUsers(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(users) | ||
} |
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.