* initial commit * add lists * update permissions * fix waypoint create * needs_full_sync for activitypub trails * fixes build issues * fix list get * further CSRF protection * update API docs * adds rate limiter * fix tiptap mentions * a bit more cleanup of main.go * Fix migration order * fixes reviewed notes * require context for activitpub server calls * improve hashing for identifier * fix copy paste error * fix sync trail/list issues * fix summit log/comment duplicates * adaptions after trail merge * fix dockerignore --------- Co-authored-by: Christian Beutel <> Co-authored-by: slothful-vassal <89943360+slothful-vassal@users.noreply.github.com>
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package routes
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/pocketbase/pocketbase/apis"
|
|
"github.com/pocketbase/pocketbase/core"
|
|
"github.com/pocketbase/pocketbase/tools/security"
|
|
)
|
|
|
|
func AuthToken(e *core.RequestEvent) error {
|
|
var data struct {
|
|
APIToken string `json:"api_token"`
|
|
}
|
|
if err := e.BindBody(&data); err != nil {
|
|
return apis.NewBadRequestError("Failed to read request data", err)
|
|
}
|
|
|
|
hashedAPIToken := security.SHA256(data.APIToken)
|
|
|
|
tokenRecord, err := e.App.FindFirstRecordByFilter(
|
|
"api_tokens",
|
|
"token = {:hash}",
|
|
map[string]any{"hash": hashedAPIToken},
|
|
)
|
|
|
|
if err != nil {
|
|
return apis.NewNotFoundError("Invalid or revoked API token", nil)
|
|
}
|
|
if !tokenRecord.GetDateTime("expiration").IsZero() &&
|
|
tokenRecord.GetDateTime("expiration").Time().Before(time.Now()) {
|
|
return apis.NewBadRequestError("Key has expired", nil)
|
|
}
|
|
|
|
tokenRecord.Set("last_used", time.Now())
|
|
if err := e.App.Save(tokenRecord); err != nil {
|
|
return err
|
|
}
|
|
|
|
userRecord, _ := e.App.FindRecordById("users", tokenRecord.GetString("user"))
|
|
token, err := userRecord.NewAuthToken()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return e.JSON(http.StatusOK, map[string]any{
|
|
"token": token,
|
|
"record": userRecord,
|
|
})
|
|
}
|