Skip to content

Commit

Permalink
refactor: Renaming service variable alias
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 26, 2023
1 parent 2a65344 commit 7216752
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 59 deletions.
12 changes: 6 additions & 6 deletions pkg/ident/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ func New(provider Provider, realm string) Service {
}
}

func (a Service) IsMatching(content string) bool {
func (s Service) IsMatching(content string) bool {
if len(content) < len(authPrefix) {
return false
}

return content[:len(authPrefix)] == authPrefix
}

func (a Service) GetUser(ctx context.Context, content string) (model.User, error) {
func (s Service) GetUser(ctx context.Context, content string) (model.User, error) {
if len(content) < len(authPrefix) {
return model.User{}, ident.ErrMalformedAuth
}
Expand All @@ -62,13 +62,13 @@ func (a Service) GetUser(ctx context.Context, content string) (model.User, error
login := strings.ToLower(data[:sepIndex])
password := strings.TrimSuffix(data[sepIndex+1:], "\n")

return a.provider.Login(ctx, login, password)
return s.provider.Login(ctx, login, password)
}

func (a Service) OnError(w http.ResponseWriter, _ *http.Request, err error) {
func (s Service) OnError(w http.ResponseWriter, _ *http.Request, err error) {
realm := ""
if len(a.realm) != 0 {
realm = fmt.Sprintf("realm=\"%s\" ", a.realm)
if len(s.realm) != 0 {
realm = fmt.Sprintf("realm=\"%s\" ", s.realm)
}

w.Header().Add("WWW-Authenticate", fmt.Sprintf("Basic %scharset=\"UTF-8\"", realm))
Expand Down
22 changes: 11 additions & 11 deletions pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func New(authProvider auth.Provider, tracerProvider trace.TracerProvider, identP
return service
}

func (a Service) Middleware(next http.Handler) http.Handler {
if len(a.identProviders) == 0 {
func (s Service) Middleware(next http.Handler) http.Handler {
if len(s.identProviders) == 0 {
return next
}

Expand All @@ -55,7 +55,7 @@ func (a Service) Middleware(next http.Handler) http.Handler {
return
}

provider, user, err := a.IsAuthenticated(r)
provider, user, err := s.IsAuthenticated(r)
if err != nil {
onHandlerFail(w, r, err, provider)
return
Expand All @@ -67,22 +67,22 @@ func (a Service) Middleware(next http.Handler) http.Handler {
})
}

func (a Service) IsAuthenticated(r *http.Request) (ident.Provider, model.User, error) {
if len(a.identProviders) == 0 {
func (s Service) IsAuthenticated(r *http.Request) (ident.Provider, model.User, error) {
if len(s.identProviders) == 0 {
return nil, model.User{}, ErrNoMatchingProvider
}

var err error

ctx, end := telemetry.StartSpan(r.Context(), a.tracer, "check_auth", trace.WithSpanKind(trace.SpanKindInternal))
ctx, end := telemetry.StartSpan(r.Context(), s.tracer, "check_auth", trace.WithSpanKind(trace.SpanKindInternal))
defer end(&err)

authContent := strings.TrimSpace(r.Header.Get("Authorization"))
if len(authContent) == 0 {
return a.identProviders[0], model.User{}, ErrEmptyAuth
return s.identProviders[0], model.User{}, ErrEmptyAuth
}

for _, provider := range a.identProviders {
for _, provider := range s.identProviders {
if !provider.IsMatching(authContent) {
continue
}
Expand All @@ -98,12 +98,12 @@ func (a Service) IsAuthenticated(r *http.Request) (ident.Provider, model.User, e
return nil, model.User{}, ErrNoMatchingProvider
}

func (a Service) IsAuthorized(ctx context.Context, profile string) bool {
if a.authProvider == nil {
func (s Service) IsAuthorized(ctx context.Context, profile string) bool {
if s.authProvider == nil {
return false
}

return a.authProvider.IsAuthorized(ctx, model.ReadUser(ctx), profile)
return s.authProvider.IsAuthorized(ctx, model.ReadUser(ctx), profile)
}

func onHandlerFail(w http.ResponseWriter, r *http.Request, err error, provider ident.Provider) {
Expand Down
28 changes: 14 additions & 14 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func New(storeService auth.Storage, authService auth.Provider) Service {
}
}

func (a Service) Get(ctx context.Context, ID uint64) (model.User, error) {
if err := a.CheckRights(ctx, ID); err != nil {
func (s Service) Get(ctx context.Context, ID uint64) (model.User, error) {
if err := s.CheckRights(ctx, ID); err != nil {
return model.User{}, err
}

item, err := a.storeService.Get(ctx, ID)
item, err := s.storeService.Get(ctx, ID)
if err != nil {
return model.User{}, fmt.Errorf("get: %w", err)
}
Expand All @@ -40,8 +40,8 @@ func (a Service) Get(ctx context.Context, ID uint64) (model.User, error) {
return item, nil
}

func (a Service) Create(ctx context.Context, user model.User) (model.User, error) {
id, err := a.storeService.Create(ctx, user)
func (s Service) Create(ctx context.Context, user model.User) (model.User, error) {
id, err := s.storeService.Create(ctx, user)
if err != nil {
return model.User{}, fmt.Errorf("create: %w", err)
}
Expand All @@ -52,39 +52,39 @@ func (a Service) Create(ctx context.Context, user model.User) (model.User, error
return user, nil
}

func (a Service) Update(ctx context.Context, user model.User) (model.User, error) {
if err := a.storeService.Update(ctx, user); err != nil {
func (s Service) Update(ctx context.Context, user model.User) (model.User, error) {
if err := s.storeService.Update(ctx, user); err != nil {
return user, fmt.Errorf("update: %w", err)
}

return user, nil
}

func (a Service) Delete(ctx context.Context, user model.User) error {
if err := a.storeService.Delete(ctx, user); err != nil {
func (s Service) Delete(ctx context.Context, user model.User) error {
if err := s.storeService.Delete(ctx, user); err != nil {
return fmt.Errorf("delete: %w", err)
}

return nil
}

func (a Service) Check(ctx context.Context, old, new model.User) error {
func (s Service) Check(ctx context.Context, old, new model.User) error {
var output []error

user := model.ReadUser(ctx)
if !old.IsZero() && user.IsZero() {
output = append(output, errors.New("you must be logged in for interacting"))
}

if new.IsZero() && !a.authService.IsAuthorized(ctx, user, "admin") {
if new.IsZero() && !s.authService.IsAuthorized(ctx, user, "admin") {
output = append(output, errors.New("you must be an admin for deleting"))
}

if new.IsZero() {
return httpModel.ConcatError(output)
}

if !old.IsZero() && !new.IsZero() && !(user.ID == new.ID || a.authService.IsAuthorized(ctx, user, "admin")) {
if !old.IsZero() && !new.IsZero() && !(user.ID == new.ID || s.authService.IsAuthorized(ctx, user, "admin")) {
output = append(output, errors.New("you're not authorized to interact with other user"))
}

Expand All @@ -99,13 +99,13 @@ func (a Service) Check(ctx context.Context, old, new model.User) error {
return httpModel.ConcatError(output)
}

func (a Service) CheckRights(ctx context.Context, id uint64) error {
func (s Service) CheckRights(ctx context.Context, id uint64) error {
user := model.ReadUser(ctx)
if user.IsZero() {
return httpModel.WrapUnauthorized(errors.New("no user in context"))
}

if id != 0 && user.ID == id || a.authService.IsAuthorized(ctx, user, "admin") {
if id != 0 && user.ID == id || s.authService.IsAuthorized(ctx, user, "admin") {
return nil
}

Expand Down
30 changes: 12 additions & 18 deletions pkg/store/db/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import (
"github.com/jackc/pgx/v5"
)

// DoAtomic do things in a transaction
func (a Service) DoAtomic(ctx context.Context, action func(context.Context) error) error {
return a.db.DoAtomic(ctx, action)
func (s Service) DoAtomic(ctx context.Context, action func(context.Context) error) error {
return s.db.DoAtomic(ctx, action)
}

const getByIDQuery = `
Expand All @@ -23,8 +22,7 @@ WHERE
id = $1
`

// Get a user
func (a Service) Get(ctx context.Context, id uint64) (model.User, error) {
func (s Service) Get(ctx context.Context, id uint64) (model.User, error) {
var item model.User
scanner := func(row pgx.Row) error {
err := row.Scan(&item.ID, &item.Login)
Expand All @@ -36,7 +34,7 @@ func (a Service) Get(ctx context.Context, id uint64) (model.User, error) {
return err
}

return item, a.db.Get(ctx, scanner, getByIDQuery, id)
return item, s.db.Get(ctx, scanner, getByIDQuery, id)
}

const insertQuery = `
Expand All @@ -51,9 +49,8 @@ INSERT INTO
) RETURNING id
`

// Create a user
func (a Service) Create(ctx context.Context, o model.User) (uint64, error) {
return a.db.Create(ctx, insertQuery, strings.ToLower(o.Login), o.Password)
func (s Service) Create(ctx context.Context, o model.User) (uint64, error) {
return s.db.Create(ctx, insertQuery, strings.ToLower(o.Login), o.Password)
}

const updateQuery = `
Expand All @@ -65,9 +62,8 @@ WHERE
id = $1
`

// Update user
func (a Service) Update(ctx context.Context, o model.User) error {
return a.db.One(ctx, updateQuery, o.ID, strings.ToLower(o.Login))
func (s Service) Update(ctx context.Context, o model.User) error {
return s.db.One(ctx, updateQuery, o.ID, strings.ToLower(o.Login))
}

const updatePasswordQuery = `
Expand All @@ -79,9 +75,8 @@ WHERE
id = $1
`

// UpdatePassword of a user
func (a Service) UpdatePassword(ctx context.Context, o model.User) error {
return a.db.One(ctx, updatePasswordQuery, o.ID, o.Password)
func (s Service) UpdatePassword(ctx context.Context, o model.User) error {
return s.db.One(ctx, updatePasswordQuery, o.ID, o.Password)
}

const deleteQuery = `
Expand All @@ -91,7 +86,6 @@ WHERE
id = $1
`

// Delete an user
func (a Service) Delete(ctx context.Context, o model.User) error {
return a.db.One(ctx, deleteQuery, o.ID)
func (s Service) Delete(ctx context.Context, o model.User) error {
return s.db.One(ctx, deleteQuery, o.ID)
}
10 changes: 4 additions & 6 deletions pkg/store/db/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ WHERE
AND password = crypt($2, password)
`

// Login checks given credentials
func (a Service) Login(ctx context.Context, login, password string) (model.User, error) {
func (s Service) Login(ctx context.Context, login, password string) (model.User, error) {
var user model.User

scanner := func(row pgx.Row) error {
return row.Scan(&user.ID, &user.Login)
}

if err := a.db.Get(ctx, scanner, readUserQuery, strings.ToLower(login), password); err != nil {
if err := s.db.Get(ctx, scanner, readUserQuery, strings.ToLower(login), password); err != nil {
slog.Error("login", "err", err, "login", login)

if err == pgx.ErrNoRows {
Expand All @@ -53,15 +52,14 @@ WHER
AND lp.login_id = $1
`

// IsAuthorized checks user on profile
func (a Service) IsAuthorized(ctx context.Context, user model.User, profile string) bool {
func (s Service) IsAuthorized(ctx context.Context, user model.User, profile string) bool {
var id uint64

scanner := func(row pgx.Row) error {
return row.Scan(&id)
}

if err := a.db.Get(ctx, scanner, readLoginProfile, user.ID, profile); err != nil {
if err := s.db.Get(ctx, scanner, readLoginProfile, user.ID, profile); err != nil {
slog.Error("authorized", "err", err, "login", user.Login)

return false
Expand Down
8 changes: 4 additions & 4 deletions pkg/store/memory/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"golang.org/x/crypto/bcrypt"
)

func (a Service) Login(_ context.Context, login, password string) (model.User, error) {
user, ok := a.ident[login]
func (s Service) Login(_ context.Context, login, password string) (model.User, error) {
user, ok := s.ident[login]
if !ok {
return model.User{}, ident.ErrInvalidCredentials
}
Expand All @@ -22,8 +22,8 @@ func (a Service) Login(_ context.Context, login, password string) (model.User, e
return user.User, nil
}

func (a Service) IsAuthorized(_ context.Context, user model.User, profile string) bool {
profiles, ok := a.auth[user.ID]
func (s Service) IsAuthorized(_ context.Context, user model.User, profile string) bool {
profiles, ok := s.auth[user.ID]
if !ok {
return false
}
Expand Down

0 comments on commit 7216752

Please sign in to comment.