Compare commits

...

2 Commits

Author SHA1 Message Date
15f95c0d74 Don't return when the unsubscribe fails
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
This rollbacks the behaviour introduced in 09eb97b
2020-03-02 13:09:13 +01:00
1197e6ce6c Move the tokens into the models
By the way, move he sqly package in the models as well
2020-03-02 13:07:04 +01:00
9 changed files with 15 additions and 22 deletions

View File

@ -8,7 +8,6 @@ import (
"git.quimbo.fr/odwrtw/canape/backend/events"
"git.quimbo.fr/odwrtw/canape/backend/models"
"git.quimbo.fr/odwrtw/canape/backend/tokens"
"git.quimbo.fr/odwrtw/canape/backend/web"
"github.com/gorilla/mux"
@ -165,7 +164,7 @@ func DeleteUserHandler(env *web.Env, w http.ResponseWriter, r *http.Request) err
}
log.Infof("deleting user tokens")
if err := tokens.DeleteUserTokens(env.Database, user.Name); err != nil {
if err := models.DeleteUserTokens(env.Database, user.Name); err != nil {
return err
}

View File

@ -7,7 +7,6 @@ import (
"time"
"git.quimbo.fr/odwrtw/canape/backend/models"
"git.quimbo.fr/odwrtw/canape/backend/tokens"
jwt "github.com/dgrijalva/jwt-go"
"github.com/jmoiron/sqlx"
@ -57,7 +56,7 @@ func (a *Authorizer) GenHash(password string) (string, error) {
}
// GenerateJWTToken generates a JWT token for a user
func (a *Authorizer) GenerateJWTToken(r *http.Request, u *models.User) (*tokens.Token, error) {
func (a *Authorizer) GenerateJWTToken(r *http.Request, u *models.User) (*models.Token, error) {
// Create a jwt token
jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
// Not before
@ -76,7 +75,7 @@ func (a *Authorizer) GenerateJWTToken(r *http.Request, u *models.User) (*tokens.
return nil, err
}
return &tokens.Token{
return &models.Token{
Token: ss,
Username: u.Name,
IP: getIPFromRequest(r),
@ -84,7 +83,7 @@ func (a *Authorizer) GenerateJWTToken(r *http.Request, u *models.User) (*tokens.
}
// Login cheks password and creates a jwt token
func (a *Authorizer) Login(r *http.Request, username, password string) (*tokens.Token, error) {
func (a *Authorizer) Login(r *http.Request, username, password string) (*models.Token, error) {
u, err := models.GetUser(a.db, username)
if err != nil {
return nil, err
@ -157,7 +156,7 @@ func (a *Authorizer) CurrentUser(rw http.ResponseWriter, req *http.Request) (*mo
}
// Check the token in database
token, err := tokens.GetUserToken(a.db, u.Name, tokenStr)
token, err := models.GetUserToken(a.db, u.Name, tokenStr)
if err != nil {
return nil, ErrInvalidToken
}

View File

@ -103,7 +103,7 @@ func (p *PolochonEventers) Unsubscribe(chanl *Channel) {
if err := tn.Unsubscribe(chanl); err != nil {
p.log.Errorf("failed to unsubscribe eventer: %s", err.Error())
return
// TODO: check if we need to return here
}
if len(tn.Subscribers()) == 0 {

View File

@ -1,4 +1,4 @@
package sqly
package models
import (
"time"

View File

@ -3,7 +3,6 @@ package models
import (
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
"git.quimbo.fr/odwrtw/canape/backend/sqly"
)
const (
@ -24,7 +23,7 @@ const (
// Media represents an external media
type Media struct {
sqly.BaseModel
BaseModel
Type string `db:"type"`
Source string `db:"source"`
Category string `db:"category"`

View File

@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"git.quimbo.fr/odwrtw/canape/backend/sqly"
"github.com/jmoiron/sqlx"
"github.com/odwrtw/papi"
)
@ -32,7 +31,7 @@ var ErrUnknownPolochon = fmt.Errorf("polochons: polochon does'nt exist")
// Polochon represents a polochon
type Polochon struct {
sqly.BaseModel
BaseModel
Name string `db:"name" json:"name"`
URL string `db:"url" json:"url"`
Token string `db:"token" json:"token"`

View File

@ -1,4 +1,4 @@
package tokens
package models
import (
"database/sql"
@ -6,7 +6,6 @@ import (
"fmt"
"time"
"git.quimbo.fr/odwrtw/canape/backend/sqly"
"github.com/jmoiron/sqlx"
)
@ -26,7 +25,7 @@ var (
// Token represents a token
type Token struct {
sqly.BaseModel
BaseModel
Username string `db:"username" json:"username"`
Token string `db:"token" json:"token"`
Description string `db:"description" json:"description"`

View File

@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"git.quimbo.fr/odwrtw/canape/backend/sqly"
"github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx/types"
"github.com/odwrtw/papi"
@ -42,7 +41,7 @@ var ErrUnknownUser = fmt.Errorf("users: user does'nt exist")
// User represents an user
type User struct {
sqly.BaseModel
BaseModel
Name string `json:"name"`
Hash string `json:"-"`
Admin bool `json:"admin"`

View File

@ -10,7 +10,6 @@ import (
"git.quimbo.fr/odwrtw/canape/backend/config"
"git.quimbo.fr/odwrtw/canape/backend/events"
"git.quimbo.fr/odwrtw/canape/backend/models"
"git.quimbo.fr/odwrtw/canape/backend/tokens"
"git.quimbo.fr/odwrtw/canape/backend/web"
"github.com/gorilla/mux"
)
@ -164,7 +163,7 @@ func EditHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error {
func GetTokensHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error {
user := auth.GetCurrentUser(r, e.Log)
tokens, err := tokens.GetUserTokens(e.Database, user.Name)
tokens, err := models.GetUserTokens(e.Database, user.Name)
if err != nil {
return err
}
@ -179,7 +178,7 @@ func DeleteTokenHandler(e *web.Env, w http.ResponseWriter, r *http.Request) erro
user := auth.GetCurrentUser(r, e.Log)
if err := tokens.DeleteToken(e.Database, user.Name, token); err != nil {
if err := models.DeleteToken(e.Database, user.Name, token); err != nil {
return err
}
@ -193,7 +192,7 @@ func EditTokenHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error
user := auth.GetCurrentUser(r, e.Log)
t, err := tokens.GetUserToken(e.Database, user.Name, token)
t, err := models.GetUserToken(e.Database, user.Name, token)
if err != nil {
return err
}