Fix for login
This commit is contained in:
8
.idea/misc.xml
generated
8
.idea/misc.xml
generated
@@ -1,8 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="SwUserDefinedSpecifications">
|
|
||||||
<option name="specTypeByUrl">
|
|
||||||
<map />
|
|
||||||
</option>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
@@ -22,7 +22,7 @@ type DatabaseConfiguration struct {
|
|||||||
Host string `yaml:"host"`
|
Host string `yaml:"host"`
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
Username string `yaml:"username"`
|
Username string `yaml:"username"`
|
||||||
Password string `yaml:"password"`
|
Password *string `yaml:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type FeaturesConfiguration struct {
|
type FeaturesConfiguration struct {
|
||||||
|
|||||||
@@ -25,13 +25,20 @@ func init() {
|
|||||||
locks = make(map[int]GameUploadToken)
|
locks = make(map[int]GameUploadToken)
|
||||||
dbConfig := config.Database()
|
dbConfig := config.Database()
|
||||||
var err error
|
var err error
|
||||||
db, err = gorm.Open(mysql.Open(
|
connectionString := ""
|
||||||
fmt.Sprintf("%s:%s@tcp(%s:%d)/transagenda?charset=utf8mb4&parseTime=True&loc=Local",
|
if dbConfig.Password != nil {
|
||||||
|
connectionString = fmt.Sprintf("%s:%s@tcp(%s:%d)/osc?charset=utf8mb4&parseTime=True&loc=Local",
|
||||||
dbConfig.Username,
|
dbConfig.Username,
|
||||||
dbConfig.Password,
|
*dbConfig.Password,
|
||||||
dbConfig.Host,
|
dbConfig.Host,
|
||||||
dbConfig.Port),
|
dbConfig.Port)
|
||||||
), &gorm.Config{
|
} else {
|
||||||
|
connectionString = fmt.Sprintf("%s@tcp(%s:%d)/osc?charset=utf8mb4&parseTime=True&loc=Local",
|
||||||
|
dbConfig.Username,
|
||||||
|
dbConfig.Host,
|
||||||
|
dbConfig.Port)
|
||||||
|
}
|
||||||
|
db, err = gorm.Open(mysql.Open(connectionString), &gorm.Config{
|
||||||
Logger: logger.New(
|
Logger: logger.New(
|
||||||
log.New(os.Stdout, "", log.LstdFlags), // io writer
|
log.New(os.Stdout, "", log.LstdFlags), // io writer
|
||||||
logger.Config{
|
logger.Config{
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ type Game struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Revision int `json:"rev"`
|
Revision int `json:"rev"`
|
||||||
PathStorage string `json:"-"`
|
PathStorage string `json:"-"`
|
||||||
Hash string `json:"hash"`
|
Hash *string `json:"hash"`
|
||||||
LastUpdate time.Time `json:"last_update"`
|
LastUpdate *time.Time `json:"last_update"`
|
||||||
UserId int `json:"-"`
|
UserId int `json:"-"`
|
||||||
Available bool `json:"available"`
|
Available bool `json:"available"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ type Credential struct {
|
|||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TokenValidation struct {
|
||||||
|
Valid bool `json:"valid"`
|
||||||
|
}
|
||||||
|
|
||||||
func Login(w http.ResponseWriter, r *http.Request) {
|
func Login(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -62,3 +66,31 @@ func Register(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
ok(payload, w, r)
|
ok(payload, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CheckToken(w http.ResponseWriter, r *http.Request) {
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
internalServerError(w, r)
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
credential := new(authentication.AccessToken)
|
||||||
|
err = json.Unmarshal(body, credential)
|
||||||
|
if err != nil {
|
||||||
|
internalServerError(w, r)
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = authentication.ParseToken(credential.Token)
|
||||||
|
if err != nil {
|
||||||
|
payload := TokenValidation{
|
||||||
|
Valid: false,
|
||||||
|
}
|
||||||
|
ok(payload, w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
payload := TokenValidation{
|
||||||
|
Valid: true,
|
||||||
|
}
|
||||||
|
ok(payload, w, r)
|
||||||
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ func Serve() {
|
|||||||
router.Route("/api", func(rApi chi.Router) {
|
router.Route("/api", func(rApi chi.Router) {
|
||||||
rApi.Route("/v1", func(r chi.Router) {
|
rApi.Route("/v1", func(r chi.Router) {
|
||||||
r.Post("/login", Login)
|
r.Post("/login", Login)
|
||||||
|
r.Post("/check/token", CheckToken)
|
||||||
if config.Features().AllowRegister {
|
if config.Features().AllowRegister {
|
||||||
r.Post("/register", Register)
|
r.Post("/register", Register)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user