Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #21 from ZupIT/feature/improve_session_validator
Browse files Browse the repository at this point in the history
Feature/improve_session_validator
  • Loading branch information
kaduartur authored Apr 14, 2020
2 parents 4962520 + 782d769 commit 6fc7b19
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 37 deletions.
50 changes: 50 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = false
max_line_length = 120
tab_width = 4
ij_continuation_indent_size = 8
ij_formatter_off_tag = @formatter:off
ij_formatter_on_tag = @formatter:on
ij_formatter_tags_enabled = false
ij_smart_tabs = false
ij_wrap_on_typing = false

[*.go]
indent_style = tab
ij_continuation_indent_size = 4
ij_go_add_leading_space_to_comments = true
ij_go_add_parentheses_for_single_import = false
ij_go_group_current_project_imports = true
ij_go_group_stdlib_imports = true
ij_go_import_sorting = gofmt
ij_go_keep_indents_on_empty_lines = false
ij_go_move_all_imports_in_one_declaration = true
ij_go_move_all_stdlib_imports_in_one_group = true
ij_go_remove_redundant_import_aliases = true
ij_go_use_back_quotes_for_imports = false

[.editorconfig]
ij_editorconfig_align_group_field_declarations = false
ij_editorconfig_space_after_colon = false
ij_editorconfig_space_after_comma = true
ij_editorconfig_space_before_colon = false
ij_editorconfig_space_before_comma = false
ij_editorconfig_spaces_around_assignment_operators = true

[{*.bash,*.sh,*.zsh}]
indent_size = 2
tab_width = 2
ij_shell_binary_ops_start_line = false
ij_shell_keep_column_alignment_padding = false
ij_shell_minify_program = false
ij_shell_redirect_followed_by_space = false
ij_shell_switch_cases_indented = false

[{*.yaml,*.yml}]
indent_size = 2
ij_yaml_keep_indents_on_empty_lines = false
ij_yaml_keep_line_breaks = true
27 changes: 8 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
module github.com/ZupIT/ritchie-cli

go 1.13
go 1.14

require (
github.com/Masterminds/semver/v3 v3.0.3
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/denisbrodbeck/machineid v1.0.1
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/fatih/color v1.9.0 // indirect
github.com/gofrs/flock v0.7.1
github.com/google/uuid v1.1.1
github.com/gosuri/uitable v0.0.4
github.com/hashicorp/go-getter v1.4.0 // indirect
github.com/manifoldco/promptui v0.3.2
github.com/matryer/is v1.2.0
github.com/matryer/moq v0.0.0-20200310130814-7721994d1b54 // indirect
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/onsi/ginkgo v1.12.0
github.com/onsi/gomega v1.9.0
github.com/manifoldco/promptui v0.7.0
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/pkg/errors v0.9.1
github.com/pkg/fileutils v0.0.0-20181114200823-d734b7f202ba // indirect
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0
github.com/spf13/cobra v1.0.0
github.com/thoas/go-funk v0.6.0
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect
gopkg.in/ini.v1 v1.49.0 // indirect
gopkg.in/square/go-jose.v2 v2.4.1 // indirect
gopkg.in/src-d/go-git.v4 v4.13.1
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
gopkg.in/square/go-jose.v2 v2.5.0 // indirect
)
1 change: 1 addition & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
whitelist = []string{
fmt.Sprintf("%s login", cmdUse),
fmt.Sprintf("%s logout", cmdUse),
fmt.Sprintf("%s help", cmdUse),
fmt.Sprintf("%s completion zsh", cmdUse),
fmt.Sprintf("%s completion bash", cmdUse),
}
Expand Down
20 changes: 15 additions & 5 deletions pkg/session/sessteam/validator_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/ZupIT/ritchie-cli/pkg/session"
"strings"
"time"

"github.com/ZupIT/ritchie-cli/pkg/session"
)

const startSession = "please, you need to start a session"

var (
ErrInvalidToken = fmt.Errorf("the access token is invalid. %s", startSession)
ErrExpiredToken = fmt.Errorf("the access token has expired. %s", startSession)
ErrDecodeToken = fmt.Errorf("unable to decode access token. %s", startSession)
ErrConvertToStruct = fmt.Errorf("couldn't convert access token into the struct. %s", startSession)
)

type Validator struct {
Expand All @@ -26,12 +36,12 @@ func (t Validator) Validate() error {
parts := strings.Split(sess.AccessToken, ".")
size := len(parts)
if size < 2 {
return fmt.Errorf("oidc: malformed jwt, expected 3 parts got %d", size)
return ErrInvalidToken
}

payload, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return fmt.Errorf("oidc: malformed jwt payload: %v", err)
return ErrDecodeToken
}

type Token struct {
Expand All @@ -40,12 +50,12 @@ func (t Validator) Validate() error {
var token Token
err = json.Unmarshal(payload, &token)
if err != nil {
return fmt.Errorf("error unmarshal token: %v", err)
return ErrConvertToStruct
}

tokenTime := time.Unix(token.Exp, 0)
if time.Since(tokenTime).Seconds() > 0 {
return fmt.Errorf("token expired, time token: %v", token.Exp)
return ErrExpiredToken
}

return nil
Expand Down
92 changes: 79 additions & 13 deletions pkg/session/sessteam/validator_team_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package sessteam

import (
"github.com/ZupIT/ritchie-cli/pkg/session"
"os"
"testing"
"time"

"github.com/dgrijalva/jwt-go"

"github.com/ZupIT/ritchie-cli/pkg/session"
)

var (
Expand All @@ -20,45 +24,107 @@ func TestMain(m *testing.M) {

func TestValidate(t *testing.T) {

type in struct {
session session.Session
exp int64
}

tests := []struct {
name string
in session.Session
in in
out error
}{
{
name: "team session",
in: session.Session{
AccessToken: "SflKxwRJSM.eKKF2QT4fwpMeJf36.POk6yJV_adQssw5c",
Organization: "zup",
Username: "dennis.ritchie",
in: in{
session: session.Session{
Organization: "zup",
Username: "dennis.ritchie",
},
exp: time.Now().Add(time.Minute * 15).Unix(),
},
out: nil,
},
{
name: "no team session",
in: session.Session{},
in: in{},
out: session.ErrNoSession,
},
{
name: "expired session token",
in: in{
session: session.Session{
Organization: "zup",
Username: "dennis.ritchie",
},
exp: time.Now().Add(time.Minute*15).Unix() - 1500,
},
out: ErrExpiredToken,
},
{
name: "invalid access token",
in: in{
session: session.Session{
AccessToken: "dasdasdasdas",
Organization: "zup",
Username: "dennis.ritchie",
},
},
out: ErrInvalidToken,
},
{
name: "decode base64 error",
in: in{
session: session.Session{
AccessToken: "ds.ds##$F/[",
Organization: "zup",
Username: "dennis.ritchie",
},
},
out: ErrDecodeToken,
},
{
name: "unmarshall error token",
in: in{
session: session.Session{
AccessToken: "eyJhbGciOiJIUzI1N.eyJzdWIilIiwiaWF0IjoxNTEIyfQ.SflKxw_adQssw5c",
Organization: "zup",
Username: "dennis.ritchie",
},
},
out: ErrConvertToStruct,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = sessionManager.Destroy()

if tt.in.Organization != "" {
err := sessionManager.Create(tt.in)
if tt.in.session.Organization != "" {

if tt.in.session.AccessToken == "" {
tt.in.session.AccessToken = generateJwt(tt.in.exp)
}

err := sessionManager.Create(tt.in.session)
if err != nil {
t.Errorf("Create(%s) got %v, want %v", tt.name, err, tt.out)
}
}

//TODO how to mock JWT Token?
/*out := tt.out
out := tt.out
got := validator.Validate()
if got != nil && got.Error() != out.Error() {
t.Errorf("Validate(%s) got %v, want %v", tt.name, got, out)
}*/

}
})
}
}

func generateJwt(exp int64) string {
atClaims := jwt.MapClaims{}
atClaims["exp"] = exp
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
token, _ := at.SignedString([]byte("Test"))
return token
}

0 comments on commit 6fc7b19

Please sign in to comment.