Skip to content

Commit

Permalink
CI CD fix: linter, deps, makefile (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-cronus authored Feb 14, 2024
1 parent df00454 commit ebaa8fb
Show file tree
Hide file tree
Showing 54 changed files with 1,064 additions and 857 deletions.
1 change: 0 additions & 1 deletion .github/workflows/CICD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ jobs:
- coin
- time
- privacy
- translations
- multimedia/picture
- analytics/tracking
- email
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ lint:
golangci-lint run

getAddLicense:
GO111MODULE=off go get -v -u github.com/google/addlicense
go install -v github.com/google/addlicense@latest

addLicense: getAddLicense
`go env GOPATH`/bin/addlicense -f LICENSE.header * .github/*
Expand Down
11 changes: 5 additions & 6 deletions analytics/tracking/tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package tracking

import (
"context"
"fmt"
"net/http"
"os"
"strings"
Expand All @@ -14,7 +13,7 @@ import (
"github.com/imroc/req/v3"
"github.com/pkg/errors"

appCfg "github.com/ice-blockchain/wintr/config"
appcfg "github.com/ice-blockchain/wintr/config"
"github.com/ice-blockchain/wintr/log"
)

Expand All @@ -26,18 +25,18 @@ func init() { //nolint:gochecknoinits // It's the only way to tweak the client.

func New(applicationYAMLKey string) Client {
var cfg config
appCfg.MustLoadFromKey(applicationYAMLKey, &cfg)
appcfg.MustLoadFromKey(applicationYAMLKey, &cfg)

if cfg.Tracking.Credentials.AppID == "" {
module := strings.ToUpper(strings.ReplaceAll(strings.ReplaceAll(applicationYAMLKey, "-", "_"), "/", "_"))
cfg.Tracking.Credentials.AppID = os.Getenv(fmt.Sprintf("%s_ANALYTICS_TRACKING_APP_ID", module))
cfg.Tracking.Credentials.AppID = os.Getenv(module + "_ANALYTICS_TRACKING_APP_ID")
if cfg.Tracking.Credentials.AppID == "" {
cfg.Tracking.Credentials.AppID = os.Getenv("ANALYTICS_TRACKING_APP_ID")
}
}
if cfg.Tracking.Credentials.APIKey == "" {
module := strings.ToUpper(strings.ReplaceAll(strings.ReplaceAll(applicationYAMLKey, "-", "_"), "/", "_"))
cfg.Tracking.Credentials.APIKey = os.Getenv(fmt.Sprintf("%s_ANALYTICS_TRACKING_API_KEY", module))
cfg.Tracking.Credentials.APIKey = os.Getenv(module + "_ANALYTICS_TRACKING_API_KEY")
if cfg.Tracking.Credentials.APIKey == "" {
cfg.Tracking.Credentials.APIKey = os.Getenv("ANALYTICS_TRACKING_API_KEY")
}
Expand Down Expand Up @@ -69,7 +68,7 @@ func (t *tracking) TrackAction(ctx context.Context, userID string, action *Actio
}

func (t *tracking) SetUserAttributes(ctx context.Context, userID string, attributes map[string]any) error {
url := t.cfg.Tracking.BaseURL + "/v1/customer/" + t.cfg.Tracking.Credentials.AppID
url := t.cfg.Tracking.BaseURL + "/v1/customer/" + t.cfg.Tracking.Credentials.AppID //nolint:goconst // .
body := make(map[string]any, 1+1+1)
body["type"] = "customer"
body["customer_id"] = userID
Expand Down
2 changes: 1 addition & 1 deletion analytics/tracking/tracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
)

func TestMain(m *testing.M) {
client = New(testApplicationYAMLKey).(*tracking) //nolint:forcetypeassert,errcheck // We know for sure.
client = New(testApplicationYAMLKey).(*tracking) //nolint:forcetypeassert,revive,errcheck // We know for sure.
os.Exit(m.Run())
}

Expand Down
18 changes: 9 additions & 9 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import (
"github.com/pkg/errors"

"github.com/ice-blockchain/wintr/auth/internal"
firebaseAuth "github.com/ice-blockchain/wintr/auth/internal/firebase"
iceAuth "github.com/ice-blockchain/wintr/auth/internal/ice"
firebaseauth "github.com/ice-blockchain/wintr/auth/internal/firebase"
iceauth "github.com/ice-blockchain/wintr/auth/internal/ice"
"github.com/ice-blockchain/wintr/time"
)

func New(ctx context.Context, applicationYAMLKey string) Client {
return &auth{
fb: firebaseAuth.New(ctx, applicationYAMLKey),
ice: iceAuth.New(applicationYAMLKey),
fb: firebaseauth.New(ctx, applicationYAMLKey),
ice: iceauth.New(applicationYAMLKey),
}
}

func (a *auth) VerifyToken(ctx context.Context, token string) (*Token, error) {
var authToken *Token
if err := iceAuth.DetectIceToken(token); err != nil {
if err := iceauth.DetectIceToken(token); err != nil {
authToken, err = a.fb.VerifyToken(ctx, token)

return authToken, errors.Wrapf(err, "can't verify fb token:%v", token)
Expand Down Expand Up @@ -68,15 +68,15 @@ func (*auth) checkMetadataOwnership(userID string, metadata jwt.MapClaims) error
func (*auth) firstRegisteredUserID(metadata map[string]any) string {
var userID string
if registeredWithProviderInterface, found := metadata[internal.RegisteredWithProviderClaim]; found {
registeredWithProvider := registeredWithProviderInterface.(string) //nolint:errcheck,forcetypeassert // Not needed.
registeredWithProvider := registeredWithProviderInterface.(string) //nolint:errcheck,revive,forcetypeassert // Not needed.
switch registeredWithProvider {
case internal.ProviderFirebase:
if firebaseIDInterface, ok := metadata[internal.FirebaseIDClaim]; ok {
userID, _ = firebaseIDInterface.(string) //nolint:errcheck // Not needed.
userID, _ = firebaseIDInterface.(string) //nolint:errcheck,revive // Not needed.
}
case internal.ProviderIce:
if iceIDInterface, ok := metadata[internal.IceIDClaim]; ok {
userID, _ = iceIDInterface.(string) //nolint:errcheck // Not needed.
userID, _ = iceIDInterface.(string) //nolint:errcheck,revive // Not needed.
}
}
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func (a *auth) GetUserUIDByEmail(ctx context.Context, email string) (string, err
return "", errors.Wrapf(err, "failed to get user by email:%v using firebase auth", email)
}

return usr.UID, err
return usr.UID, nil
}

func (a *auth) GenerateTokens( //nolint:revive // We need to have these parameters.
Expand Down
22 changes: 11 additions & 11 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func TestDeleteUser_Success(t *testing.T) {
require.NoError(t, err)
require.NotEmpty(t, user.PhoneNumber)
require.NoError(t, client.DeleteUser(ctx, uid))
require.NoError(t, client.DeleteUser(ctx, uuid.NewString()), ErrUserNotFound)
require.NotErrorIs(t, client.DeleteUser(ctx, uuid.NewString()), ErrUserNotFound)
_, err = fixture.GetUser(ctx, uid)
require.NotNil(t, err)
require.Error(t, err)
require.True(t, strings.HasPrefix(err.Error(), "no user exists with the"))
}

Expand Down Expand Up @@ -132,7 +132,7 @@ func TestVerifyIceToken_ValidToken(t *testing.T) {
assert.Equal(t, role, verifiedAccessToken.Claims["role"])

_, err = client.VerifyToken(ctx, refreshToken)
require.Error(t, err, ErrWrongTypeToken)
require.ErrorIs(t, err, ErrWrongTypeToken)
}

func TestVerifyIceToken_InvalidToken(t *testing.T) {
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestGenerateIceTokens_Valid(t *testing.T) {
assert.Equal(t, role, verifiedAccessToken.Claims["role"])
assert.Equal(t, deviceID, verifiedAccessToken.Claims["deviceUniqueID"])
_, err = client.VerifyToken(ctx, refreshToken)
require.Error(t, err, ErrWrongTypeToken)
require.ErrorIs(t, err, ErrWrongTypeToken)
}

func TestUpdateCustomClaims_Ice(t *testing.T) {
Expand All @@ -184,7 +184,7 @@ func TestUpdateCustomClaims_Ice(t *testing.T) {
"role": "author",
}
)
require.Error(t, client.UpdateCustomClaims(ctx, userID, claims), ErrUserNotFound)
require.ErrorIs(t, client.UpdateCustomClaims(ctx, userID, claims), ErrUserNotFound)
}

func TestDeleteUser_Ice(t *testing.T) {
Expand All @@ -193,7 +193,7 @@ func TestDeleteUser_Ice(t *testing.T) {
defer cancel()
userID := "ice"

assert.Nil(t, client.DeleteUser(ctx, userID))
require.NoError(t, client.DeleteUser(ctx, userID))
}

func TestParseToken_Parse(t *testing.T) { //nolint:funlen // .
Expand Down Expand Up @@ -258,7 +258,7 @@ func TestMetadata_Empty(t *testing.T) {
var decodedMetadata jwt.MapClaims
err = client.(*auth).ice.VerifyTokenFields(metadataToken, &decodedMetadata) //nolint:forcetypeassert // .
require.NoError(t, err)
assert.Equal(t, 3, len(decodedMetadata))
assert.Len(t, decodedMetadata, 3)
assert.Equal(t, userID, decodedMetadata["sub"])
assert.Equal(t, internal.MetadataIssuer, decodedMetadata["iss"])
assert.Equal(t, now.Unix(), int64(decodedMetadata["iat"].(float64))) //nolint:forcetypeassert // .
Expand All @@ -268,7 +268,7 @@ func TestMetadata_Empty(t *testing.T) {
assert.Equal(t, userID, tok.UserID)
err = client.(*auth).ice.VerifyTokenFields(metadataToken, &decodedMetadata) //nolint:forcetypeassert // .
require.NoError(t, err)
assert.Equal(t, 3, len(decodedMetadata))
assert.Len(t, decodedMetadata, 3)
assert.Equal(t, userID, decodedMetadata["sub"])
assert.Equal(t, internal.MetadataIssuer, decodedMetadata["iss"])
assert.Equal(t, now.Unix(), int64(decodedMetadata["iat"].(float64))) //nolint:forcetypeassert // .
Expand Down Expand Up @@ -296,7 +296,7 @@ func TestMetadata_RegisteredBy(t *testing.T) { //nolint:funlen // .
var decodedMetadata jwt.MapClaims
err = client.(*auth).ice.VerifyTokenFields(metadataToken, &decodedMetadata) //nolint:forcetypeassert // .
require.NoError(t, err)
assert.Equal(t, 6, len(decodedMetadata))
assert.Len(t, decodedMetadata, 6)
assert.Equal(t, userID, decodedMetadata["sub"])
assert.Equal(t, internal.MetadataIssuer, decodedMetadata["iss"])
assert.Equal(t, now.Unix(), int64(decodedMetadata["iat"].(float64))) //nolint:forcetypeassert // .
Expand All @@ -311,7 +311,7 @@ func TestMetadata_RegisteredBy(t *testing.T) { //nolint:funlen // .

err = client.(*auth).ice.VerifyTokenFields(metadataToken, &decodedMetadata) //nolint:forcetypeassert // .
require.NoError(t, err)
assert.Equal(t, 6, len(decodedMetadata))
assert.Len(t, decodedMetadata, 6)
assert.Equal(t, userID, decodedMetadata["sub"])
assert.Equal(t, internal.MetadataIssuer, decodedMetadata["iss"])
assert.Equal(t, now.Unix(), int64(decodedMetadata["iat"].(float64))) //nolint:forcetypeassert // .
Expand Down Expand Up @@ -350,5 +350,5 @@ func TestMetadata_MetadataNotOwnedByToken(t *testing.T) {

tok := &Token{UserID: uuid.NewString()} // Metadata was issued for token "userID", not random one.
_, err = client.ModifyTokenWithMetadata(tok, metadataToken)
require.Error(t, err, ErrInvalidToken)
require.ErrorIs(t, err, ErrWrongTypeToken)
}
16 changes: 8 additions & 8 deletions auth/fixture/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
stdlibtime "time"

firebase "firebase.google.com/go/v4"
firebaseAuth "firebase.google.com/go/v4/auth"
firebaseauth "firebase.google.com/go/v4/auth"
"github.com/goccy/go-json"
"github.com/google/uuid"
"github.com/pkg/errors"
Expand All @@ -28,13 +28,13 @@ import (

//nolint:gochecknoglobals // We're using lazy stateless singletons for the whole testing runtime.
var (
globalFirebaseClient *firebaseAuth.Client
globalFirebaseClient *firebaseauth.Client
globalIceClient iceauth.Client
singletonIce = new(sync.Once)
singletonFirebase = new(sync.Once)
)

func clientFirebase() *firebaseAuth.Client {
func clientFirebase() *firebaseauth.Client {
singletonFirebase.Do(func() {
globalFirebaseClient = newFirebaseClient()
})
Expand Down Expand Up @@ -63,7 +63,7 @@ func CreateUser(role string) (uid, token string) {
})
log.Panic(err) //nolint:revive // Intended.

url := fmt.Sprintf("https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=%s", os.Getenv("GCP_FIREBASE_AUTH_API_KEY"))
url := "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=" + os.Getenv("GCP_FIREBASE_AUTH_API_KEY")
respBytes := postRequest(url, req)

var respBody struct {
Expand All @@ -81,7 +81,7 @@ func DeleteUser(uid string) {
log.Panic(clientFirebase().DeleteUser(delCtx, uid))
}

func GetUser(ctx context.Context, uid string) (*firebaseAuth.UserRecord, error) {
func GetUser(ctx context.Context, uid string) (*firebaseauth.UserRecord, error) {
return clientFirebase().GetUser(ctx, uid) //nolint:wrapcheck // It's a proxy.
}

Expand Down Expand Up @@ -115,7 +115,7 @@ func generateUser(ctx context.Context, role string) (uid, email, password string
phoneNumber := fmt.Sprintf("+1%d", randNumber.Uint64()+phoneNumberMin)
password = uuid.NewString()

user := new(firebaseAuth.UserToCreate).
user := new(firebaseauth.UserToCreate).
Email(fmt.Sprintf("%s@%s-test-user.com", uuid.NewString(), uuid.NewString())).
Password(password).
PhoneNumber(phoneNumber).
Expand Down Expand Up @@ -145,7 +145,7 @@ func postRequest(url string, req []byte) []byte {
return bodyBytes
}

func newFirebaseClient() *firebaseAuth.Client {
func newFirebaseClient() *firebaseauth.Client {
ctx := context.Background()
fileContent := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
filePath := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
Expand All @@ -165,7 +165,7 @@ func newFirebaseClient() *firebaseAuth.Client {
eagerLoadCtx, cancelEagerLoad := context.WithTimeout(ctx, 5*stdlibtime.Second) //nolint:gomnd // It's a one time call.
defer cancelEagerLoad()
t, err := client.VerifyIDTokenAndCheckRevoked(eagerLoadCtx, "invalid token")
if t != nil || !firebaseAuth.IsIDTokenInvalid(err) {
if t != nil || !firebaseauth.IsIDTokenInvalid(err) {
log.Panic(errors.New("unexpected success"))
}

Expand Down
22 changes: 11 additions & 11 deletions auth/internal/firebase/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import (

"dario.cat/mergo"
firebase "firebase.google.com/go/v4"
firebaseAuth "firebase.google.com/go/v4/auth"
firebaseauth "firebase.google.com/go/v4/auth"
"github.com/pkg/errors"
firebaseoption "google.golang.org/api/option"

"github.com/ice-blockchain/wintr/auth/internal"
appCfg "github.com/ice-blockchain/wintr/config"
appcfg "github.com/ice-blockchain/wintr/config"
"github.com/ice-blockchain/wintr/log"
)

func New(ctx context.Context, applicationYAMLKey string) Client {
cfg := new(config)
appCfg.MustLoadFromKey(applicationYAMLKey, cfg)
appcfg.MustLoadFromKey(applicationYAMLKey, cfg)
cfg.setWintrAuthFirebaseCredentialsFileContent(applicationYAMLKey)
cfg.setWintrAuthFirebaseCredentialsFilePath(applicationYAMLKey)

Expand All @@ -41,7 +41,7 @@ func New(ctx context.Context, applicationYAMLKey string) Client {
eagerLoadCtx, cancelEagerLoad := context.WithTimeout(ctx, 30*stdlibtime.Second) //nolint:gomnd // It's a one time call.
defer cancelEagerLoad()
t, err := client.VerifyIDTokenAndCheckRevoked(eagerLoadCtx, "invalid token")
if t != nil || !firebaseAuth.IsIDTokenInvalid(err) {
if t != nil || !firebaseauth.IsIDTokenInvalid(err) {
log.Panic(errors.New("unexpected success"))
}

Expand All @@ -63,10 +63,10 @@ func (a *auth) VerifyToken(ctx context.Context, token string) (*internal.Token,
userID := firebaseToken.UID
if len(firebaseToken.Claims) > 0 {
if emailInterface, found := firebaseToken.Claims["email"]; found {
email, _ = emailInterface.(string) //nolint:errcheck // Not needed.
email, _ = emailInterface.(string) //nolint:errcheck,revive // Not needed.
}
if roleInterface, found := firebaseToken.Claims["role"]; found {
role, _ = roleInterface.(string) //nolint:errcheck // Not needed.
role, _ = roleInterface.(string) //nolint:errcheck,revive // Not needed.
}
}

Expand Down Expand Up @@ -109,7 +109,7 @@ func (a *auth) UpdateEmail(ctx context.Context, userID, email string) error {
if ctx.Err() != nil {
return errors.Wrap(ctx.Err(), "context failed")
}
if _, err := a.client.UpdateUser(ctx, userID, new(firebaseAuth.UserToUpdate).Email(email).EmailVerified(true)); err != nil {
if _, err := a.client.UpdateUser(ctx, userID, new(firebaseauth.UserToUpdate).Email(email).EmailVerified(true)); err != nil {
if strings.HasSuffix(err.Error(), "user with the provided email already exists") {
return ErrConflict
}
Expand Down Expand Up @@ -138,13 +138,13 @@ func (a *auth) DeleteUser(ctx context.Context, userID string) error {
return nil
}

func (a *auth) GetUser(ctx context.Context, userID string) (*firebaseAuth.UserRecord, error) {
func (a *auth) GetUser(ctx context.Context, userID string) (*firebaseauth.UserRecord, error) {
usr, err := a.client.GetUser(ctx, userID)

return usr, errors.Wrapf(err, "can't get firebase user data for:%v", userID)
}

func (a *auth) GetUserByEmail(ctx context.Context, email string) (*firebaseAuth.UserRecord, error) {
func (a *auth) GetUserByEmail(ctx context.Context, email string) (*firebaseauth.UserRecord, error) {
usr, err := a.client.GetUserByEmail(ctx, email)
if err != nil {
if strings.HasSuffix(err.Error(), fmt.Sprintf("no user exists with the email: \"%v\"", email)) {
Expand All @@ -160,7 +160,7 @@ func (a *auth) GetUserByEmail(ctx context.Context, email string) (*firebaseAuth.
func (cfg *config) setWintrAuthFirebaseCredentialsFileContent(applicationYAMLKey string) {
if cfg.WintrAuthFirebase.Credentials.FileContent == "" {
module := strings.ToUpper(strings.ReplaceAll(strings.ReplaceAll(applicationYAMLKey, "-", "_"), "/", "_"))
cfg.WintrAuthFirebase.Credentials.FileContent = os.Getenv(fmt.Sprintf("%s_AUTH_CREDENTIALS_FILE_CONTENT", module))
cfg.WintrAuthFirebase.Credentials.FileContent = os.Getenv(module + "_AUTH_CREDENTIALS_FILE_CONTENT")
if cfg.WintrAuthFirebase.Credentials.FileContent == "" {
cfg.WintrAuthFirebase.Credentials.FileContent = os.Getenv("AUTH_CREDENTIALS_FILE_CONTENT")
}
Expand All @@ -176,7 +176,7 @@ func (cfg *config) setWintrAuthFirebaseCredentialsFileContent(applicationYAMLKey
func (cfg *config) setWintrAuthFirebaseCredentialsFilePath(applicationYAMLKey string) {
if cfg.WintrAuthFirebase.Credentials.FilePath == "" {
module := strings.ToUpper(strings.ReplaceAll(strings.ReplaceAll(applicationYAMLKey, "-", "_"), "/", "_"))
cfg.WintrAuthFirebase.Credentials.FilePath = os.Getenv(fmt.Sprintf("%s_AUTH_CREDENTIALS_FILE_PATH", module))
cfg.WintrAuthFirebase.Credentials.FilePath = os.Getenv(module + "_AUTH_CREDENTIALS_FILE_PATH")
if cfg.WintrAuthFirebase.Credentials.FilePath == "" {
cfg.WintrAuthFirebase.Credentials.FilePath = os.Getenv("AUTH_CREDENTIALS_FILE_PATH")
}
Expand Down
Loading

0 comments on commit ebaa8fb

Please sign in to comment.