Skip to content

Commit

Permalink
feat(token): add refresh token info (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
wass3r authored Jan 11, 2021
1 parent cd87586 commit d3bb371
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 165 deletions.
11 changes: 11 additions & 0 deletions constants/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package constants

// Constants for tokens.
const (
// RefreshTokenName is the name associated with the refresh token.
RefreshTokenName = "vela_refresh_token"
)
40 changes: 26 additions & 14 deletions database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ var (
// User type has an empty Name field provided.
ErrEmptyUserName = errors.New("empty user name provided")

// ErrEmptyUserRefreshToken defines the error type when a
// User type has an empty RefreshToken field provided.
ErrEmptyUserRefreshToken = errors.New("empty user refresh token provided")

// ErrEmptyUserToken defines the error type when a
// User type has an empty Token field provided.
ErrEmptyUserToken = errors.New("empty user token provided")
Expand All @@ -42,13 +46,14 @@ var (

// User is the database representation of a user.
type User struct {
ID sql.NullInt64 `sql:"id"`
Name sql.NullString `sql:"name"`
Token sql.NullString `sql:"token"`
Hash sql.NullString `sql:"hash"`
Favorites pq.StringArray `sql:"favorites"`
Active sql.NullBool `sql:"active"`
Admin sql.NullBool `sql:"admin"`
ID sql.NullInt64 `sql:"id"`
Name sql.NullString `sql:"name"`
RefreshToken sql.NullString `sql:"refresh_token"`
Token sql.NullString `sql:"token"`
Hash sql.NullString `sql:"hash"`
Favorites pq.StringArray `sql:"favorites"`
Active sql.NullBool `sql:"active"`
Admin sql.NullBool `sql:"admin"`
}

// Nullify ensures the valid flag for
Expand All @@ -72,6 +77,11 @@ func (u *User) Nullify() *User {
u.Name.Valid = false
}

// check if the RefreshToken field should be false
if len(u.RefreshToken.String) == 0 {
u.RefreshToken.Valid = false
}

// check if the Token field should be false
if len(u.Token.String) == 0 {
u.Token.Valid = false
Expand All @@ -92,6 +102,7 @@ func (u *User) ToLibrary() *library.User {

user.SetID(u.ID.Int64)
user.SetName(u.Name.String)
user.SetRefreshToken(u.RefreshToken.String)
user.SetToken(u.Token.String)
user.SetHash(u.Hash.String)
user.SetActive(u.Active.Bool)
Expand Down Expand Up @@ -142,13 +153,14 @@ func (u *User) Validate() error {
// to a database User type.
func UserFromLibrary(u *library.User) *User {
user := &User{
ID: sql.NullInt64{Int64: u.GetID(), Valid: true},
Name: sql.NullString{String: u.GetName(), Valid: true},
Token: sql.NullString{String: u.GetToken(), Valid: true},
Hash: sql.NullString{String: u.GetHash(), Valid: true},
Active: sql.NullBool{Bool: u.GetActive(), Valid: true},
Admin: sql.NullBool{Bool: u.GetAdmin(), Valid: true},
Favorites: u.GetFavorites(),
ID: sql.NullInt64{Int64: u.GetID(), Valid: true},
Name: sql.NullString{String: u.GetName(), Valid: true},
RefreshToken: sql.NullString{String: u.GetRefreshToken(), Valid: true},
Token: sql.NullString{String: u.GetToken(), Valid: true},
Hash: sql.NullString{String: u.GetHash(), Valid: true},
Active: sql.NullBool{Bool: u.GetActive(), Valid: true},
Admin: sql.NullBool{Bool: u.GetAdmin(), Valid: true},
Favorites: u.GetFavorites(),
}

return user.Nullify()
Expand Down
46 changes: 26 additions & 20 deletions database/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ func TestDatabase_User_Nullify(t *testing.T) {
var u *User

want := &User{
ID: sql.NullInt64{Int64: 0, Valid: false},
Name: sql.NullString{String: "", Valid: false},
Token: sql.NullString{String: "", Valid: false},
Hash: sql.NullString{String: "", Valid: false},
Active: sql.NullBool{Bool: false, Valid: false},
Admin: sql.NullBool{Bool: false, Valid: false},
ID: sql.NullInt64{Int64: 0, Valid: false},
Name: sql.NullString{String: "", Valid: false},
RefreshToken: sql.NullString{String: "", Valid: false},
Token: sql.NullString{String: "", Valid: false},
Hash: sql.NullString{String: "", Valid: false},
Active: sql.NullBool{Bool: false, Valid: false},
Admin: sql.NullBool{Bool: false, Valid: false},
}

// setup tests
Expand Down Expand Up @@ -61,6 +62,7 @@ func TestDatabase_User_ToLibrary(t *testing.T) {

want.SetID(1)
want.SetName("octocat")
want.SetRefreshToken("superSecretRefreshToken")
want.SetToken("superSecretToken")
want.SetHash("superSecretHash")
want.SetFavorites([]string{"github/octocat"})
Expand Down Expand Up @@ -104,18 +106,20 @@ func TestDatabase_User_Validate(t *testing.T) {
{ // no hash set for user
failure: true,
user: &User{
ID: sql.NullInt64{Int64: 1, Valid: true},
Name: sql.NullString{String: "octocat", Valid: true},
Token: sql.NullString{String: "superSecretToken", Valid: true},
ID: sql.NullInt64{Int64: 1, Valid: true},
Name: sql.NullString{String: "octocat", Valid: true},
RefreshToken: sql.NullString{String: "superSecretRefreshToken", Valid: true},
Token: sql.NullString{String: "superSecretToken", Valid: true},
},
},
{ // invalid name set for user
failure: true,
user: &User{
ID: sql.NullInt64{Int64: 1, Valid: true},
Name: sql.NullString{String: "!@#$%^&*()", Valid: true},
Token: sql.NullString{String: "superSecretToken", Valid: true},
Hash: sql.NullString{String: "superSecretHash", Valid: true},
ID: sql.NullInt64{Int64: 1, Valid: true},
Name: sql.NullString{String: "!@#$%^&*()", Valid: true},
RefreshToken: sql.NullString{String: "superSecretRefreshToken", Valid: true},
Token: sql.NullString{String: "superSecretToken", Valid: true},
Hash: sql.NullString{String: "superSecretHash", Valid: true},
},
},
{ // invalid favorites set for user
Expand Down Expand Up @@ -154,6 +158,7 @@ func TestDatabase_UserFromLibrary(t *testing.T) {

u.SetID(1)
u.SetName("octocat")
u.SetRefreshToken("superSecretRefreshToken")
u.SetToken("superSecretToken")
u.SetHash("superSecretHash")
u.SetFavorites([]string{"github/octocat"})
Expand All @@ -174,13 +179,14 @@ func TestDatabase_UserFromLibrary(t *testing.T) {
// type with all fields set to a fake value.
func testUser() *User {
return &User{
ID: sql.NullInt64{Int64: 1, Valid: true},
Name: sql.NullString{String: "octocat", Valid: true},
Token: sql.NullString{String: "superSecretToken", Valid: true},
Hash: sql.NullString{String: "superSecretHash", Valid: true},
Favorites: []string{"github/octocat"},
Active: sql.NullBool{Bool: true, Valid: true},
Admin: sql.NullBool{Bool: false, Valid: true},
ID: sql.NullInt64{Int64: 1, Valid: true},
Name: sql.NullString{String: "octocat", Valid: true},
RefreshToken: sql.NullString{String: "superSecretRefreshToken", Valid: true},
Token: sql.NullString{String: "superSecretToken", Valid: true},
Hash: sql.NullString{String: "superSecretHash", Valid: true},
Favorites: []string{"github/octocat"},
Active: sql.NullBool{Bool: true, Valid: true},
Admin: sql.NullBool{Bool: false, Valid: true},
}
}

Expand Down
90 changes: 1 addition & 89 deletions library/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

import "fmt"
Expand All @@ -21,49 +20,7 @@ import "fmt"
//
// swagger:model Login
type Login struct {
Username *string `json:"username,omitempty"`
Password *string `json:"password,omitempty"`
OTP *string `json:"otp,omitempty"`
Token *string `json:"token,omitempty"`
}

// GetUsername returns the Username field.
//
// When the provided Login type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (l *Login) GetUsername() string {
// return zero value if Login type or Username field is nil
if l == nil || l.Username == nil {
return ""
}

return *l.Username
}

// GetPassword returns the Password field.
//
// When the provided Login type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (l *Login) GetPassword() string {
// return zero value if Login type or Password field is nil
if l == nil || l.Password == nil {
return ""
}

return *l.Password
}

// GetOTP returns the Username field.
//
// When the provided Login type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (l *Login) GetOTP() string {
// return zero value if Login type or OTP field is nil
if l == nil || l.OTP == nil {
return ""
}

return *l.OTP
Token *string `json:"token,omitempty"`
}

// GetToken returns the Token field.
Expand All @@ -79,45 +36,6 @@ func (l *Login) GetToken() string {
return *l.Token
}

// SetUsername sets the Username field.
//
// When the provided Login type is nil, it
// will set nothing and immediately return.
func (l *Login) SetUsername(v string) {
// return if Login type is nil
if l == nil {
return
}

l.Username = &v
}

// SetPassword sets the Password field.
//
// When the provided Login type is nil, it
// will set nothing and immediately return.
func (l *Login) SetPassword(v string) {
// return if Login type is nil
if l == nil {
return
}

l.Password = &v
}

// SetOTP sets the OTP field.
//
// When the provided Login type is nil, it
// will set nothing and immediately return.
func (l *Login) SetOTP(v string) {
// return if Login type is nil
if l == nil {
return
}

l.OTP = &v
}

// SetToken sets the Token field.
//
// When the provided Login type is nil, it
Expand All @@ -134,14 +52,8 @@ func (l *Login) SetToken(v string) {
// String implements the Stringer interface for the Login type.
func (l *Login) String() string {
return fmt.Sprintf(`{
OTP: %s,
Password: %s,
Token: %s,
Username: %s,
}`,
l.GetOTP(),
l.GetPassword(),
l.GetToken(),
l.GetUsername(),
)
}
33 changes: 0 additions & 33 deletions library/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ func TestLibrary_Login_Getters(t *testing.T) {

// run tests
for _, test := range tests {
if test.login.GetUsername() != test.want.GetUsername() {
t.Errorf("GetUsername is %v, want %v", test.login.GetUsername(), test.want.GetUsername())
}

if test.login.GetPassword() != test.want.GetPassword() {
t.Errorf("GetPassword is %v, want %v", test.login.GetPassword(), test.want.GetPassword())
}

if test.login.GetOTP() != test.want.GetOTP() {
t.Errorf("GetOTP is %v, want %v", test.login.GetOTP(), test.want.GetOTP())
}

if test.login.GetToken() != test.want.GetToken() {
t.Errorf("GetToken is %v, want %v", test.login.GetToken(), test.want.GetToken())
}
Expand Down Expand Up @@ -77,20 +65,8 @@ func TestLibrary_Login_Setters(t *testing.T) {

// run tests
for _, test := range tests {
test.login.SetUsername(test.want.GetUsername())
test.login.SetPassword(test.want.GetPassword())
test.login.SetOTP(test.want.GetOTP())
test.login.SetToken(test.want.GetToken())

if test.login.GetUsername() != test.want.GetUsername() {
t.Errorf("SetUsername is %v, want %v", test.login.GetUsername(), test.want.GetUsername())
}
if test.login.GetPassword() != test.want.GetPassword() {
t.Errorf("SetPassword is %v, want %v", test.login.GetPassword(), test.want.GetPassword())
}
if test.login.GetOTP() != test.want.GetOTP() {
t.Errorf("SetOTP is %v, want %v", test.login.GetOTP(), test.want.GetOTP())
}
if test.login.GetToken() != test.want.GetToken() {
t.Errorf("SetToken is %v, want %v", test.login.GetToken(), test.want.GetToken())
}
Expand All @@ -102,15 +78,9 @@ func TestLogin_String(t *testing.T) {
l := testLogin()

want := fmt.Sprintf(`{
OTP: %s,
Password: %s,
Token: %s,
Username: %s,
}`,
l.GetOTP(),
l.GetPassword(),
l.GetToken(),
l.GetUsername(),
)

// run test
Expand All @@ -126,9 +96,6 @@ func TestLogin_String(t *testing.T) {
func testLogin() *Login {
l := new(Login)

l.SetUsername("octocat")
l.SetPassword("superSecretPassword")
l.SetOTP("123456")
l.SetToken("superSecretToken")

return l
Expand Down
Loading

0 comments on commit d3bb371

Please sign in to comment.