mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 12:39:17 +00:00
adding account reset
This commit is contained in:
parent
2f36fa3a2c
commit
03fbf2934b
@ -13,11 +13,6 @@ import (
|
||||
"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) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
@ -58,11 +53,6 @@ func RemoveAccountApp(w http.ResponseWriter, r *http.Request) {
|
||||
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) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
func AddAccount(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
token, res := BearerAccountToken(r);
|
||||
if res != nil || token.TokenType != "create" {
|
||||
if res != nil || token.TokenType != APP_ACCOUNTCREATE {
|
||||
ErrResponse(w, http.StatusUnauthorized, res)
|
||||
return
|
||||
}
|
||||
|
@ -25,15 +25,15 @@ func AddAccountApp(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
accountToken := store.AccountToken{
|
||||
AccountID: id,
|
||||
TokenType: "attach",
|
||||
TokenType: APP_ACCOUNTATTACH,
|
||||
Token: token,
|
||||
Expires: time.Now().Unix() + APP_ATTACHEXPIRE,
|
||||
};
|
||||
}
|
||||
if err := store.DB.Create(&accountToken).Error; err != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
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) {
|
||||
|
||||
token, res := BearerAccountToken(r);
|
||||
if res != nil || token.TokenType != "attach" {
|
||||
LogMsg("invalid bearer token")
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
if res != nil || token.TokenType != APP_ACCOUNTATTACH {
|
||||
ErrResponse(w, http.StatusUnauthorized, res)
|
||||
return
|
||||
}
|
||||
|
||||
// parse app data
|
||||
var appData AppData
|
||||
if ParseRequest(r, w, &appData) != nil {
|
||||
LogMsg("invalid request data")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if res = ParseRequest(r, w, &appData); res != nil {
|
||||
ErrResponse(w, http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
// gernate app token
|
||||
data, err := securerandom.Bytes(APP_TOKENSIZE)
|
||||
if err != nil {
|
||||
LogMsg("failed to generate token")
|
||||
w.WriteHeader(http.StatusInternalServerError);
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
access := hex.EncodeToString(data)
|
||||
@ -55,8 +52,7 @@ func SetAccountApp(w http.ResponseWriter, r *http.Request) {
|
||||
return nil;
|
||||
});
|
||||
if err != nil {
|
||||
LogMsg("failed to save app")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
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_ATTACHEXPIRE = 300
|
||||
const APP_CREATEEXPIRE = 86400
|
||||
const APP_RESETEXPIRE = 86400
|
||||
const APP_CONNECTEXPIRE = 30
|
||||
const APP_KEYSIZE = 4096
|
||||
const APP_RSA4096 = "RSA4096"
|
||||
@ -40,6 +41,9 @@ const APP_QUEUEAUDIO = "audio"
|
||||
const APP_QUEUEVIDEO = "video"
|
||||
const APP_QUEUEPHOTO = "photo"
|
||||
const APP_QUEUEDEFAULT = ""
|
||||
const APP_ACCOUNTATTACH = "attach"
|
||||
const APP_ACCOUNTCREATE = "create"
|
||||
const APP_ACCOUNTRESET = "reset"
|
||||
|
||||
func AppCardStatus(status string) bool {
|
||||
if status == APP_CARDPENDING {
|
||||
|
@ -48,7 +48,7 @@ type AccountToken struct {
|
||||
Token string `gorm:"not null;uniqueIndex"`
|
||||
Expires int64 `gorm:"not null"`
|
||||
Created int64 `gorm:"autoCreateTime"`
|
||||
Account Account
|
||||
Account *Account
|
||||
}
|
||||
|
||||
// NOTE: card & app reference account by guid, all other tables by id
|
||||
|
Loading…
Reference in New Issue
Block a user