From 30ea24b760855033e68eb16d78432e34c2c553f7 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 18 Apr 2022 10:21:37 +0700 Subject: [PATCH] feat: add metadata to MFAError (#23) --- .github/workflows/semantic-pr.yaml | 3 ++- examples/graphql_flow/challenges/dummy.go | 12 ++++++++---- examples/graphql_flow/flows/single_flow.go | 4 ++-- examples/graphql_flow/go.mod | 2 +- examples/graphql_flow/go.sum | 2 ++ examples/graphql_flow/graph/schema.resolvers.go | 6 +++--- .../challenges/dummy.go | 7 +++++-- .../challenges/dummy2.go | 7 +++++-- mfa/entities/error.go | 5 +++-- 9 files changed, 31 insertions(+), 17 deletions(-) diff --git a/.github/workflows/semantic-pr.yaml b/.github/workflows/semantic-pr.yaml index cc51e5d..34da00e 100644 --- a/.github/workflows/semantic-pr.yaml +++ b/.github/workflows/semantic-pr.yaml @@ -9,9 +9,10 @@ on: jobs: main: - name: Validate PR title + name: Semantic Pull Request runs-on: ubuntu-latest steps: - uses: amannn/action-semantic-pull-request@v4 + name: Semantic Pull Request env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/examples/graphql_flow/challenges/dummy.go b/examples/graphql_flow/challenges/dummy.go index ef16eb5..2e5d858 100644 --- a/examples/graphql_flow/challenges/dummy.go +++ b/examples/graphql_flow/challenges/dummy.go @@ -1,13 +1,14 @@ package challenges import ( - "errors" + "context" "log" "math/rand" "time" "github.com/honestbank/mfa-lib/challenge" "github.com/honestbank/mfa-lib/challenge/entities" + mfaEntities "github.com/honestbank/mfa-lib/mfa/entities" ) var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") @@ -25,16 +26,19 @@ type DummyChallenge struct { Seed string `json:"seed"` } -func (c *DummyChallenge) Solve(body map[string]interface{}) (*map[string]interface{}, error) { +func (c *DummyChallenge) Solve(ctx context.Context, body map[string]interface{}) (*map[string]interface{}, error) { log.Println("seed:", c.Seed) log.Println("password:", body["code"]) if body["reference"] == c.Seed && body["code"] == "123" { return nil, nil } - return nil, errors.New("failed!") + return nil, &mfaEntities.MFAError{ + Code: "failed", + Message: "failed", + } } -func (c *DummyChallenge) Request(body map[string]interface{}) (*map[string]interface{}, error) { +func (c *DummyChallenge) Request(ctx context.Context, body map[string]interface{}) (*map[string]interface{}, error) { rand.Seed(time.Now().UnixNano()) c.Seed = randSeq(10) log.Println("Seed:", c.Seed) diff --git a/examples/graphql_flow/flows/single_flow.go b/examples/graphql_flow/flows/single_flow.go index 9968821..fced2e6 100644 --- a/examples/graphql_flow/flows/single_flow.go +++ b/examples/graphql_flow/flows/single_flow.go @@ -26,9 +26,9 @@ func (f SingleFlow) Initialize(ctx context.Context) (*JWTEntities.JWTAdditions, }, nil } -func (f SingleFlow) Validate(ctx context.Context, challenge string, JWTData mfaEntities.JWTData, challengeInput *string) error { +func (f SingleFlow) Validate(ctx context.Context, challenge string, JWTData mfaEntities.JWTData, challengeInput *string) (context.Context, error) { //TODO implement me - return nil + return ctx, nil } func (f SingleFlow) Resolve(jwtData mfaEntities.JWTData) (*map[string]interface{}, error) { diff --git a/examples/graphql_flow/go.mod b/examples/graphql_flow/go.mod index 3eddeb1..a082f09 100644 --- a/examples/graphql_flow/go.mod +++ b/examples/graphql_flow/go.mod @@ -4,7 +4,7 @@ go 1.17 require ( github.com/99designs/gqlgen v0.15.1 - github.com/honestbank/mfa-lib v1.5.2 + github.com/honestbank/mfa-lib v1.10.1 github.com/vektah/gqlparser/v2 v2.2.0 ) diff --git a/examples/graphql_flow/go.sum b/examples/graphql_flow/go.sum index f035084..05213c6 100644 --- a/examples/graphql_flow/go.sum +++ b/examples/graphql_flow/go.sum @@ -22,6 +22,8 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/honestbank/mfa-lib v1.10.1 h1:AShZsLrew0sHPjTA67F47sAATA0/Il4AXfiu9Xjxnmw= +github.com/honestbank/mfa-lib v1.10.1/go.mod h1:YQhuFfNvaJbSoAEgJf6Img4o2DiKgKgK3FZ+hsuaWTc= github.com/kevinmbeaulieu/eq-go v1.0.0/go.mod h1:G3S8ajA56gKBZm4UB9AOyoOS37JO3roToPzKNM8dtdM= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= diff --git a/examples/graphql_flow/graph/schema.resolvers.go b/examples/graphql_flow/graph/schema.resolvers.go index 8ae6865..9decee7 100644 --- a/examples/graphql_flow/graph/schema.resolvers.go +++ b/examples/graphql_flow/graph/schema.resolvers.go @@ -14,7 +14,7 @@ import ( ) func (r *mutationResolver) InitializeFlow(ctx context.Context, flowName string) (*model.InitializeFlowResponse, error) { - result, err := r.MFAService.Request(ctx, flowName) + result, err := r.MFAService.Request(ctx, flowName, nil) if err != nil { return nil, err } @@ -30,7 +30,7 @@ func (r *mutationResolver) ChallengesSolveOtp(ctx context.Context, input *model. return nil, err } log.Println(string(jsonInput)) - result, err := r.MFAService.Process(ctx, ctx.Value("jwt").(string), "dummy", string(jsonInput), false) + result, err := r.MFAService.Process(ctx, ctx.Value("jwt").(string), "dummy", string(jsonInput), false, nil) if err != nil { return nil, err } @@ -42,7 +42,7 @@ func (r *mutationResolver) ChallengesSolveOtp(ctx context.Context, input *model. } func (r *mutationResolver) ChallengesRequestOtp(ctx context.Context) (*model.RequestOTPResult, error) { - result, err := r.MFAService.Process(ctx, ctx.Value("jwt").(string), "dummy", "{}", true) + result, err := r.MFAService.Process(ctx, ctx.Value("jwt").(string), "dummy", "{}", true, nil) if err != nil { return nil, err } diff --git a/examples/single_flow_multiple_challenges/challenges/dummy.go b/examples/single_flow_multiple_challenges/challenges/dummy.go index 686d4cf..be0c947 100644 --- a/examples/single_flow_multiple_challenges/challenges/dummy.go +++ b/examples/single_flow_multiple_challenges/challenges/dummy.go @@ -2,13 +2,13 @@ package challenges import ( "context" - "errors" "log" "math/rand" "time" "github.com/honestbank/mfa-lib/challenge" "github.com/honestbank/mfa-lib/challenge/entities" + mfaEntities "github.com/honestbank/mfa-lib/mfa/entities" ) var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") @@ -32,7 +32,10 @@ func (c *DummyChallenge) Solve(ctx context.Context, body map[string]interface{}) if body["username"] == "admin" && body["password"].(string) == c.Seed { return nil, nil } - return nil, errors.New("failed!") + return nil, &mfaEntities.MFAError{ + Code: "failed", + Message: "failed", + } } func (c *DummyChallenge) Request(ctx context.Context, body map[string]interface{}) (*map[string]interface{}, error) { diff --git a/examples/single_flow_multiple_challenges/challenges/dummy2.go b/examples/single_flow_multiple_challenges/challenges/dummy2.go index 0878cd1..ef8ac38 100644 --- a/examples/single_flow_multiple_challenges/challenges/dummy2.go +++ b/examples/single_flow_multiple_challenges/challenges/dummy2.go @@ -2,13 +2,13 @@ package challenges import ( "context" - "errors" "log" "math/rand" "time" "github.com/honestbank/mfa-lib/challenge" "github.com/honestbank/mfa-lib/challenge/entities" + mfaEntities "github.com/honestbank/mfa-lib/mfa/entities" ) type DummyTwoChallenge struct { @@ -22,7 +22,10 @@ func (c *DummyTwoChallenge) Solve(ctx context.Context, body map[string]interface if body["username"] == "admin" && body["password"].(string) == c.Seed { return nil, nil } - return nil, errors.New("failed!") + return nil, &mfaEntities.MFAError{ + Code: "failed", + Message: "failed", + } } func (c *DummyTwoChallenge) Request(ctx context.Context, body map[string]interface{}) (*map[string]interface{}, error) { diff --git a/mfa/entities/error.go b/mfa/entities/error.go index df424e2..a1861a1 100644 --- a/mfa/entities/error.go +++ b/mfa/entities/error.go @@ -1,8 +1,9 @@ package entities type MFAError struct { - Code string - Message string + Code string + Message string + Metadata interface{} } func (e MFAError) Error() string {