44 lines
919 B
Go
44 lines
919 B
Go
|
|
package routes
|
||
|
|
|
||
|
|
import (
|
||
|
|
"crypto/rand"
|
||
|
|
"encoding/hex"
|
||
|
|
"gifuu/tools"
|
||
|
|
"net/http"
|
||
|
|
"strconv"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func GET_Challenge(w http.ResponseWriter, r *http.Request) {
|
||
|
|
query := r.URL.Query()
|
||
|
|
|
||
|
|
paramDifficulty, _ := strconv.Atoi(query.Get("difficulty"))
|
||
|
|
if paramDifficulty < 18 {
|
||
|
|
tools.SendClientError(w, r, tools.ERROR_CHALLENGE_TOO_EASY)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create Session
|
||
|
|
nonce := make([]byte, 16)
|
||
|
|
if _, err := rand.Read(nonce); err != nil {
|
||
|
|
tools.SendServerError(w, r, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
normal := hex.EncodeToString(nonce)
|
||
|
|
expiry := time.Now().Add(5 * time.Minute).Unix()
|
||
|
|
|
||
|
|
// Store Session
|
||
|
|
tools.ChallengeAtomic.Lock()
|
||
|
|
tools.ChallengeSession[normal] = tools.ChallengeSessionData{
|
||
|
|
Expires: expiry,
|
||
|
|
Difficulty: paramDifficulty,
|
||
|
|
}
|
||
|
|
tools.ChallengeAtomic.Unlock()
|
||
|
|
|
||
|
|
tools.SendJSON(w, r, http.StatusOK, map[string]any{
|
||
|
|
"nonce": normal,
|
||
|
|
"difficulty": paramDifficulty,
|
||
|
|
"expires": expiry,
|
||
|
|
})
|
||
|
|
}
|