diff --git a/library/template.go b/library/template.go index 90c7f7ab..78c0e955 100644 --- a/library/template.go +++ b/library/template.go @@ -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 ( diff --git a/library/user.go b/library/user.go index 18386eda..c8ce3648 100644 --- a/library/user.go +++ b/library/user.go @@ -4,7 +4,11 @@ package library -import "fmt" +import ( + "fmt" + + "github.com/go-vela/types/constants" +) // User is the library representation of a user. // @@ -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 { diff --git a/library/user_test.go b/library/user_test.go index 39c6fddb..58161b3a 100644 --- a/library/user_test.go +++ b/library/user_test.go @@ -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{