start work on accounts module

This commit is contained in:
tsmethurst
2021-03-23 13:17:54 +01:00
parent aa9ce272dc
commit 7139116e5d
7 changed files with 133 additions and 6 deletions

View File

@ -19,19 +19,56 @@
package account
import (
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/gotosocial/gotosocial/internal/config"
"github.com/gotosocial/gotosocial/internal/db"
"github.com/gotosocial/gotosocial/internal/gtsmodel"
"github.com/gotosocial/gotosocial/internal/module"
"github.com/gotosocial/gotosocial/internal/module/oauth"
"github.com/gotosocial/gotosocial/internal/router"
)
const (
basePath = "/api/v1/accounts"
basePathWithID = basePath + "/:id"
verifyPath = basePath + "/verify_credentials"
)
type accountModule struct {
config *config.Config
db db.DB
}
// New returns a new account module
func New() module.ClientAPIModule {
return &accountModule{}
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 {
r.AttachHandler(http.MethodGet, verifyPath, m.AccountVerifyGETHandler)
return nil
}
func (m *accountModule) AccountVerifyGETHandler(c *gin.Context) {
s := sessions.Default(c)
userID, ok := s.Get(oauth.SessionAuthorizedUser).(string)
if !ok || userID == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "The access token is invalid"})
return
}
acct := &gtsmodel.Account{}
if err := m.db.GetAccountByUserID(userID, acct); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err})
return
}
c.JSON(http.StatusOK, acct.ToMastoSensitive())
}

View File

@ -51,6 +51,7 @@ const (
authSignInPath = "/auth/sign_in"
oauthTokenPath = "/oauth/token"
oauthAuthorizePath = "/oauth/authorize"
SessionAuthorizedUser = "authorized_user"
)
// oauthModule is an oauth2 oauthModule that satisfies the ClientAPIModule interface
@ -209,7 +210,7 @@ func (m *oauthModule) appsPOSTHandler(c *gin.Context) {
}
// done, return the new app information per the spec here: https://docs.joinmastodon.org/methods/apps/
c.JSON(http.StatusOK, app.ToMastotype())
c.JSON(http.StatusOK, app.ToMasto())
}
// signInGETHandler should be served at https://example.org/auth/sign_in.
@ -411,7 +412,7 @@ func (m *oauthModule) oauthTokenMiddleware(c *gin.Context) {
l.Trace("entering OauthTokenMiddleware")
if ti, err := m.oauthServer.ValidationBearerToken(c.Request); err == nil {
l.Tracef("authenticated user %s with bearer token, scope is %s", ti.GetUserID(), ti.GetScope())
c.Set("authenticated_user", ti.GetUserID())
c.Set(SessionAuthorizedUser, ti.GetUserID())
} else {
l.Trace("continuing with unauthenticated request")