Skip to content

Commit

Permalink
Merge pull request #27 from ystv/liam.burnand/adding-officerships
Browse files Browse the repository at this point in the history
Adding officerships to web-auth
  • Loading branch information
COMTOP1 authored Aug 20, 2024
2 parents 61495e1 + 4668ace commit b4050ff
Show file tree
Hide file tree
Showing 57 changed files with 5,245 additions and 515 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-template-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v3
- uses: WillAbides/setup-go-faster@v1
with:
go-version: '1.22'
go-version: '1.22.5'
- name: download go-template-lint
run: go install sourcegraph.com/sourcegraph/go-template-lint@latest
- name: run go-template-lint
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v3
- uses: WillAbides/setup-go-faster@v1
with:
go-version: '1.22'
go-version: '1.22.5'
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nilaway.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v3
- uses: WillAbides/setup-go-faster@v1
with:
go-version: '1.22'
go-version: '1.22.5'
- name: download nilaway
run: go install go.uber.org/nilaway/cmd/nilaway@latest
- name: run nilaway
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ linters:
- sql
disable:
- wrapcheck
- goerr113
- err113
- depguard
- tagalign
- godot
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22.4-alpine3.20 AS build
FROM golang:1.22.5-alpine3.20 AS build
LABEL site="auth"
LABEL stage="builder"

Expand Down
79 changes: 66 additions & 13 deletions api/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,116 @@ import (
"fmt"
"time"

sq "github.com/Masterminds/squirrel"
"gopkg.in/guregu/null.v4"

"github.com/ystv/web-auth/utils"
)

// getTokens will get the tokens for a user
func (s *Store) getTokens(ctx context.Context, userID int) ([]Token, error) {
var t []Token
err := s.db.SelectContext(ctx, &t, `SELECT *
FROM web_auth.api_tokens
WHERE user_id = $1;`, userID)

builder := utils.PSQL().Select("*").
From("web_auth.api_tokens").
Where(sq.Eq{"user_id": userID})

sql, args, err := builder.ToSql()
if err != nil {
panic(fmt.Errorf("failed to build sql for getTokens: %w", err))
}

err = s.db.SelectContext(ctx, &t, sql, args...)
if err != nil {
return nil, fmt.Errorf("error at getTokens: %w", err)
return nil, fmt.Errorf("failed to get tokens: %w", err)
}

return t, nil
}

// getToken will get a specific token
func (s *Store) getToken(ctx context.Context, t1 Token) (Token, error) {
var t Token
err := s.db.GetContext(ctx, &t, `SELECT *
FROM web_auth.api_tokens
WHERE token_id = $1
LIMIT 1;`, t1.TokenID)

builder := utils.PSQL().Select("*").
From("web_auth.api_tokens").
Where(sq.Eq{"token_id": t1.TokenID}).
Limit(1)

sql, args, err := builder.ToSql()
if err != nil {
panic(fmt.Errorf("failed to build sql for getToken: %w", err))
}

err = s.db.GetContext(ctx, &t, sql, args...)
if err != nil {
return t, fmt.Errorf("error at getToken: %w", err)
return t, fmt.Errorf("failed to get token: %w", err)
}

return t, nil
}

// addToken will add a token specified in the parameter
func (s *Store) addToken(ctx context.Context, t1 Token) (Token, error) {
var t Token
stmt, err := s.db.PrepareNamedContext(ctx, "INSERT INTO web_auth.api_tokens (token_id, name, description, expiry, user_id) VALUES (:token_id, :name, :description, :expiry, :user_id) RETURNING token_id, name, description, expiry, user_id")

builder := utils.PSQL().Insert("web_auth.api_tokens").
Columns("token_id", "name", "description", "expiry", "user_id").
Values(t1.TokenID, t1.Name, t1.Description, t1.Expiry, t1.UserID).
Suffix("RETURNING token_id, name, description, expiry, user_id")

sql, args, err := builder.ToSql()
if err != nil {
panic(fmt.Errorf("failed to build sql for addToken: %w", err))
}

stmt, err := s.db.PrepareContext(ctx, sql)
if err != nil {
return Token{}, fmt.Errorf("failed to add token: %w", err)
}

defer stmt.Close()
err = stmt.Get(&t, t1)

err = stmt.QueryRow(args...).Scan(&t)
if err != nil {
return Token{}, fmt.Errorf("failed to add token: %w", err)
}

return t, nil
}

// deleteToken will delete a token and prevent it from being used
func (s *Store) deleteToken(ctx context.Context, t Token) error {
_, err := s.db.NamedExecContext(ctx, `DELETE FROM web_auth.api_tokens WHERE token_id = :token_id`, t)
builder := utils.PSQL().Delete("web_auth.api_tokens").
Where(sq.Eq{"token_id": t.TokenID})

sql, args, err := builder.ToSql()
if err != nil {
panic(fmt.Errorf("failed to build sql for deletToken: %w", err))
}

_, err = s.db.ExecContext(ctx, sql, args...)
if err != nil {
return fmt.Errorf("failed to delete token: %w", err)
}

return nil
}

// deleteOldToken will delete all expired tokens
func (s *Store) deleteOldToken(ctx context.Context) error {
_, err := s.db.ExecContext(ctx, `DELETE FROM web_auth.api_tokens WHERE expiry <= $1`, null.TimeFrom(time.Now()))
builder := utils.PSQL().Delete("web_auth.api_tokens").
Where(sq.LtOrEq{"expiry": null.TimeFrom(time.Now())})

sql, args, err := builder.ToSql()
if err != nil {
panic(fmt.Errorf("failed to build sql for deleteOldToken: %w", err))
}

_, err = s.db.ExecContext(ctx, sql, args...)
if err != nil {
return fmt.Errorf("failed to delete old token: %w", err)
}

return nil
}
30 changes: 15 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ module github.com/ystv/web-auth

go 1.22

toolchain go1.22.4
toolchain go1.22.5

require (
github.com/Clarilab/gocloaksession v1.14.0
github.com/Masterminds/squirrel v1.5.4
github.com/Nerzal/gocloak/v8 v8.6.0
github.com/balacode/zr-whirl v1.0.2
github.com/dustin/go-humanize v1.0.1
github.com/go-playground/validator/v10 v10.21.0
github.com/go-playground/validator/v10 v10.22.0
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/gorilla/schema v1.3.0
github.com/gorilla/schema v1.4.1
github.com/gorilla/securecookie v1.1.2
github.com/gorilla/sessions v1.2.2
github.com/gorilla/sessions v1.3.0
github.com/jinzhu/copier v0.4.0
github.com/jmoiron/sqlx v1.4.0
github.com/joho/godotenv v1.5.1
Expand All @@ -27,15 +27,15 @@ require (
gopkg.in/guregu/null.v4 v4.0.0
)

require github.com/go-resty/resty/v2 v2.13.1 // indirect
require github.com/go-resty/resty/v2 v2.14.0 // indirect

require (
github.com/Nerzal/gocloak/v13 v13.9.0 // indirect
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 // indirect
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-test/deep v1.1.0 // indirect
github.com/go-test/deep v1.1.1 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
Expand All @@ -48,12 +48,12 @@ require (
github.com/toorop/go-dkim v0.0.0-20240103092955-90b7d1423f92 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240610135401-a8a62080eff3 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
Loading

0 comments on commit b4050ff

Please sign in to comment.