mirror of
https://github.com/balzack/databag.git
synced 2025-02-15 21:19:16 +00:00
adding account reset
This commit is contained in:
parent
2f36fa3a2c
commit
03fbf2934b
@ -13,11 +13,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddAccountAuthentication(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAccountApps(w http.ResponseWriter, r *http.Request) {
|
func GetAccountApps(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
@ -58,11 +53,6 @@ func RemoveAccountApp(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetAccountAuthentication(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetAccountExport(w http.ResponseWriter, r *http.Request) {
|
func SetAccountExport(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
func AddAccount(w http.ResponseWriter, r *http.Request) {
|
func AddAccount(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
token, res := BearerAccountToken(r);
|
token, res := BearerAccountToken(r);
|
||||||
if res != nil || token.TokenType != "create" {
|
if res != nil || token.TokenType != APP_ACCOUNTCREATE {
|
||||||
ErrResponse(w, http.StatusUnauthorized, res)
|
ErrResponse(w, http.StatusUnauthorized, res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -25,15 +25,15 @@ func AddAccountApp(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
accountToken := store.AccountToken{
|
accountToken := store.AccountToken{
|
||||||
AccountID: id,
|
AccountID: id,
|
||||||
TokenType: "attach",
|
TokenType: APP_ACCOUNTATTACH,
|
||||||
Token: token,
|
Token: token,
|
||||||
Expires: time.Now().Unix() + APP_ATTACHEXPIRE,
|
Expires: time.Now().Unix() + APP_ATTACHEXPIRE,
|
||||||
};
|
}
|
||||||
if err := store.DB.Create(&accountToken).Error; err != nil {
|
if err := store.DB.Create(&accountToken).Error; err != nil {
|
||||||
ErrResponse(w, http.StatusInternalServerError, err)
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
WriteResponse(w, token);
|
WriteResponse(w, token)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
41
net/server/internal/api_addAccountAuthentication.go
Normal file
41
net/server/internal/api_addAccountAuthentication.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package databag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
"encoding/hex"
|
||||||
|
"databag/internal/store"
|
||||||
|
"github.com/theckman/go-securerandom"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddAccountAuthentication(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
id, err := AccountLogin(r)
|
||||||
|
if err != nil {
|
||||||
|
ErrResponse(w, http.StatusUnauthorized, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data, res := securerandom.Bytes(4)
|
||||||
|
if res != nil {
|
||||||
|
ErrResponse(w, http.StatusInternalServerError, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
token := hex.EncodeToString(data)
|
||||||
|
|
||||||
|
accountToken := store.AccountToken{
|
||||||
|
AccountID: id,
|
||||||
|
TokenType: APP_ACCOUNTRESET,
|
||||||
|
Token: token,
|
||||||
|
Expires: time.Now().Unix() + APP_RESETEXPIRE,
|
||||||
|
}
|
||||||
|
if err := store.DB.Create(&accountToken).Error; err != nil {
|
||||||
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
WriteResponse(w, token)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -11,25 +11,22 @@ import (
|
|||||||
func SetAccountApp(w http.ResponseWriter, r *http.Request) {
|
func SetAccountApp(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
token, res := BearerAccountToken(r);
|
token, res := BearerAccountToken(r);
|
||||||
if res != nil || token.TokenType != "attach" {
|
if res != nil || token.TokenType != APP_ACCOUNTATTACH {
|
||||||
LogMsg("invalid bearer token")
|
ErrResponse(w, http.StatusUnauthorized, res)
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse app data
|
// parse app data
|
||||||
var appData AppData
|
var appData AppData
|
||||||
if ParseRequest(r, w, &appData) != nil {
|
if res = ParseRequest(r, w, &appData); res != nil {
|
||||||
LogMsg("invalid request data")
|
ErrResponse(w, http.StatusBadRequest, res)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// gernate app token
|
// gernate app token
|
||||||
data, err := securerandom.Bytes(APP_TOKENSIZE)
|
data, err := securerandom.Bytes(APP_TOKENSIZE)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogMsg("failed to generate token")
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
w.WriteHeader(http.StatusInternalServerError);
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
access := hex.EncodeToString(data)
|
access := hex.EncodeToString(data)
|
||||||
@ -55,8 +52,7 @@ func SetAccountApp(w http.ResponseWriter, r *http.Request) {
|
|||||||
return nil;
|
return nil;
|
||||||
});
|
});
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogMsg("failed to save app")
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
37
net/server/internal/api_setAccountAuthentication.go
Normal file
37
net/server/internal/api_setAccountAuthentication.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package databag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"databag/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetAccountAuthentication(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
token, res := BearerAccountToken(r)
|
||||||
|
if res != nil || token.TokenType != APP_ACCOUNTRESET {
|
||||||
|
ErrResponse(w, http.StatusUnauthorized, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if token.Account == nil {
|
||||||
|
ErrResponse(w, http.StatusUnauthorized, errors.New("invalid reset token"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
username, password, ret := BasicCredentials(r)
|
||||||
|
if ret != nil {
|
||||||
|
ErrResponse(w, http.StatusUnauthorized, ret)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
token.Account.Username = username;
|
||||||
|
token.Account.Password = password;
|
||||||
|
if err := store.DB.Save(token.Account).Error; err != nil {
|
||||||
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
WriteResponse(w, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -5,6 +5,7 @@ const APP_BODYLIMIT = 1048576
|
|||||||
const APP_VERSION = "0.0.1"
|
const APP_VERSION = "0.0.1"
|
||||||
const APP_ATTACHEXPIRE = 300
|
const APP_ATTACHEXPIRE = 300
|
||||||
const APP_CREATEEXPIRE = 86400
|
const APP_CREATEEXPIRE = 86400
|
||||||
|
const APP_RESETEXPIRE = 86400
|
||||||
const APP_CONNECTEXPIRE = 30
|
const APP_CONNECTEXPIRE = 30
|
||||||
const APP_KEYSIZE = 4096
|
const APP_KEYSIZE = 4096
|
||||||
const APP_RSA4096 = "RSA4096"
|
const APP_RSA4096 = "RSA4096"
|
||||||
@ -40,6 +41,9 @@ const APP_QUEUEAUDIO = "audio"
|
|||||||
const APP_QUEUEVIDEO = "video"
|
const APP_QUEUEVIDEO = "video"
|
||||||
const APP_QUEUEPHOTO = "photo"
|
const APP_QUEUEPHOTO = "photo"
|
||||||
const APP_QUEUEDEFAULT = ""
|
const APP_QUEUEDEFAULT = ""
|
||||||
|
const APP_ACCOUNTATTACH = "attach"
|
||||||
|
const APP_ACCOUNTCREATE = "create"
|
||||||
|
const APP_ACCOUNTRESET = "reset"
|
||||||
|
|
||||||
func AppCardStatus(status string) bool {
|
func AppCardStatus(status string) bool {
|
||||||
if status == APP_CARDPENDING {
|
if status == APP_CARDPENDING {
|
||||||
|
@ -48,7 +48,7 @@ type AccountToken struct {
|
|||||||
Token string `gorm:"not null;uniqueIndex"`
|
Token string `gorm:"not null;uniqueIndex"`
|
||||||
Expires int64 `gorm:"not null"`
|
Expires int64 `gorm:"not null"`
|
||||||
Created int64 `gorm:"autoCreateTime"`
|
Created int64 `gorm:"autoCreateTime"`
|
||||||
Account Account
|
Account *Account
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: card & app reference account by guid, all other tables by id
|
// NOTE: card & app reference account by guid, all other tables by id
|
||||||
|
Loading…
Reference in New Issue
Block a user