-
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
19 changed files
with
304 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import os | ||
import binascii | ||
|
||
def generate_api_key(): | ||
return binascii.hexlify(os.urandom(16)).decode() | ||
|
||
print(generate_api_key()) |
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,76 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"web/model" | ||
|
||
"github.com/gin-contrib/sessions" | ||
"github.com/gin-gonic/gin" | ||
"github.com/gin-gonic/gin/render" | ||
) | ||
|
||
type loginFormData struct { | ||
Email string `form:"email"` | ||
Username string `form:"username"` | ||
Password string `form:"password"` | ||
} | ||
|
||
func Signup(c *gin.Context) { | ||
var data loginFormData | ||
if err := c.Bind(&data); err != nil { | ||
c.Render(http.StatusBadRequest, render.Data{}) | ||
return | ||
} | ||
|
||
if !model.CheckUserExistance(data.Email) { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "User already exists"}) | ||
return | ||
} | ||
|
||
user := model.CreateUser(data.Email, data.Username, data.Password) | ||
if user == nil || user.ID == 0 { | ||
c.Render(http.StatusBadRequest, render.Data{}) | ||
return | ||
} | ||
|
||
session := sessions.Default(c) | ||
session.Set("userID", user.ID) | ||
if err := session.Save(); err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save session"}) | ||
return | ||
} | ||
c.Redirect(http.StatusFound, os.Getenv("API_PATH")+"ping") | ||
} | ||
|
||
func Signin(c *gin.Context) { | ||
var data loginFormData | ||
if err := c.Bind(&data); err != nil { | ||
c.Render(http.StatusBadRequest, render.Data{}) | ||
return | ||
} | ||
|
||
user := model.CheckPasswordMatch(data.Username, data.Password) | ||
if user.ID == 0 { | ||
c.Render(http.StatusUnauthorized, render.Data{}) | ||
return | ||
} | ||
|
||
session := sessions.Default(c) | ||
session.Set("userID", user.ID) | ||
if err := session.Save(); err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save session"}) | ||
return | ||
} | ||
c.Redirect(http.StatusFound, os.Getenv("API_PATH")+"ping") | ||
} | ||
|
||
func Signout(c *gin.Context) { | ||
session := sessions.Default(c) | ||
session.Clear() | ||
if err := session.Save(); err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save session"}) | ||
return | ||
} | ||
c.Redirect(http.StatusFound, "/") | ||
} |
File renamed without changes.
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,17 @@ | ||
package helper | ||
|
||
import ( | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
func EncryptPassword(password string) (string, error) { | ||
encryptedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(encryptedPassword), nil | ||
} | ||
|
||
func CheckPasswordMatch(hashedPassword, password string) error { | ||
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) | ||
} |
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,37 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"web/model" | ||
|
||
"github.com/gin-contrib/sessions" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func Authentication() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
// before request | ||
session := sessions.Default(c) | ||
sessionID := session.Get("userID") | ||
|
||
if sessionID == nil { | ||
// Check for API key in the request | ||
apiKey := c.GetHeader("X-API-Key") | ||
if apiKey != os.Getenv("STATUSPAGE_API_KEY") { | ||
c.AbortWithStatus(http.StatusUnauthorized) | ||
return | ||
} | ||
} else { | ||
userID := sessionID.(uint) | ||
user, err := model.GetUserByID(userID) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusUnauthorized) | ||
return | ||
} | ||
c.Set("userID", user.ID) | ||
} | ||
|
||
c.Next() | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,16 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
CREATE TABLE users ( | ||
CREATE TABLE IF NOT EXISTS users ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
INSERT INTO users (name) VALUES ('Alice'); | ||
INSERT INTO users (created_at, updated_at, email, username, password) | ||
VALUES (NOW(), NOW(), 'statuspage@api.com', 'StatusPageAPI', 'b63729771dbc8166067f73c735c7a78dde61ccbe25d05d0cefec4ba2acbcfa23'); | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
DROP TABLE users; | ||
-- +goose StatementEnd | ||
-- +goose StatementEnd |
Oops, something went wrong.