gotosocial/internal/module/account/account.go

89 lines
2.6 KiB
Go
Raw Normal View History

2021-03-01 15:41:43 +01:00
/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package account
import (
2021-03-23 22:13:01 +01:00
"fmt"
2021-03-23 13:17:54 +01:00
"net/http"
"github.com/gin-gonic/gin"
"github.com/gotosocial/gotosocial/internal/config"
"github.com/gotosocial/gotosocial/internal/db"
2021-03-23 22:13:01 +01:00
"github.com/gotosocial/gotosocial/internal/db/model"
"github.com/gotosocial/gotosocial/internal/module"
2021-03-23 13:17:54 +01:00
"github.com/gotosocial/gotosocial/internal/module/oauth"
"github.com/gotosocial/gotosocial/internal/router"
)
2021-03-23 13:17:54 +01:00
const (
basePath = "/api/v1/accounts"
basePathWithID = basePath + "/:id"
verifyPath = basePath + "/verify_credentials"
)
type accountModule struct {
2021-03-23 13:17:54 +01:00
config *config.Config
db db.DB
}
// New returns a new account module
2021-03-23 13:17:54 +01:00
func New(config *config.Config, db db.DB) module.ClientAPIModule {
return &accountModule{
config: config,
db: db,
}
}
// Route attaches all routes from this module to the given router
func (m *accountModule) Route(r router.Router) error {
2021-03-23 13:17:54 +01:00
r.AttachHandler(http.MethodGet, verifyPath, m.AccountVerifyGETHandler)
return nil
}
2021-03-23 13:17:54 +01:00
2021-03-23 22:13:01 +01:00
// AccountVerifyGETHandler serves a user's account details to them IF they reached this
// handler while in possession of a valid token, according to the oauth middleware.
2021-03-23 13:17:54 +01:00
func (m *accountModule) AccountVerifyGETHandler(c *gin.Context) {
2021-03-23 22:13:01 +01:00
i, ok := c.Get(oauth.SessionAuthorizedUser)
fmt.Println(i)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "The access token is invalid"})
return
}
userID, ok := (i).(string)
2021-03-23 13:17:54 +01:00
if !ok || userID == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "The access token is invalid"})
return
}
2021-03-23 22:13:01 +01:00
acct := &model.Account{}
2021-03-23 13:17:54 +01:00
if err := m.db.GetAccountByUserID(userID, acct); err != nil {
2021-03-23 22:13:01 +01:00
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
acctSensitive, err := m.db.AccountToMastoSensitive(acct)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
2021-03-23 13:17:54 +01:00
}
2021-03-23 22:13:01 +01:00
c.JSON(http.StatusOK, acctSensitive)
2021-03-23 13:17:54 +01:00
}