Skip to content

Commit

Permalink
feat: add user sanitize function (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
wass3r authored Jan 19, 2021
1 parent 682d103 commit b7233df
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 0 additions & 1 deletion library/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// Use of this source code is governed by the LICENSE file in this repository.

// nolint:dupl // false positive due to similarity with login.go
package library

import (
Expand Down
25 changes: 24 additions & 1 deletion library/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

package library

import "fmt"
import (
"fmt"

"github.com/go-vela/types/constants"
)

// User is the library representation of a user.
//
Expand All @@ -20,6 +24,25 @@ type User struct {
Admin *bool `json:"admin,omitempty"`
}

// Sanitize creates a duplicate of the User without the token values.
func (u *User) Sanitize() *User {
// create a variable since constants can not be addressable
//
// https://golang.org/ref/spec#Address_operators
value := constants.SecretMask

return &User{
ID: u.ID,
Name: u.Name,
RefreshToken: &value,
Token: &value,
Hash: &value,
Favorites: u.Favorites,
Active: u.Active,
Admin: u.Admin,
}
}

// Environment returns a list of environment variables
// provided from the fields of the User type.
func (u *User) Environment() map[string]string {
Expand Down
19 changes: 19 additions & 0 deletions library/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,27 @@ import (
"fmt"
"reflect"
"testing"

"github.com/go-vela/types/constants"
)

func TestLibrary_User_Sanitize(t *testing.T) {
// setup types
u := testUser()

want := testUser()
want.SetHash(constants.SecretMask)
want.SetToken(constants.SecretMask)
want.SetRefreshToken(constants.SecretMask)

// run test
got := u.Sanitize()

if !reflect.DeepEqual(got, want) {
t.Errorf("Sanitize is %v, want %v", got, want)
}
}

func TestLibrary_User_Environment(t *testing.T) {
// setup types
want := map[string]string{
Expand Down

0 comments on commit b7233df

Please sign in to comment.