Skip to content

Commit

Permalink
Merge 5d5dadc into fc43b36
Browse files Browse the repository at this point in the history
  • Loading branch information
mortezaalizadeh committed Feb 15, 2021
2 parents fc43b36 + 5d5dadc commit 40521b5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require (
github.com/golang/mock v1.4.4
github.com/lestrrat-go/jwx v1.1.1
github.com/lucsky/cuid v1.0.2
github.com/micro-business/go-core v0.3.4
github.com/micro-business/gokit-core v0.3.4
github.com/micro-business/go-core v0.4.0
github.com/micro-business/gokit-core v0.4.0
github.com/onsi/ginkgo v1.15.0
github.com/onsi/gomega v1.10.5
github.com/prometheus/client_golang v1.9.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/micro-business/go-core v0.3.4 h1:Al6Om6l8E9KBJe4FAhYPWSETEixn7diWXEWUKPT07Bo=
github.com/micro-business/go-core v0.3.4/go.mod h1:rTu31M26tvDDSdQ1Bw0oGsbgnpacJoz3QMJOuv8z92I=
github.com/micro-business/gokit-core v0.3.4 h1:Ze5yz3/k3Vh/ocGgV0Kt36E+w6OkOh1B8dlgvp/UYJA=
github.com/micro-business/gokit-core v0.3.4/go.mod h1:mp8wvBR3AVlYPM9Oprm7xv35b7b8mxOkpAwIx5LIo5Q=
github.com/micro-business/go-core v0.4.0 h1:xuU8843T5P8Gbt3uvIEuB10TJ3e+Hdb8IgwtcRXPJj8=
github.com/micro-business/go-core v0.4.0/go.mod h1:FlcnEPvpQ+7s9wAIJ3Z6pt5BI/X3Y7odxGttXMBxiCA=
github.com/micro-business/gokit-core v0.4.0 h1:x8uLuAQD8NSMakQOjfHCXTAsjrGD8n4XQ0gBLMWt1XI=
github.com/micro-business/gokit-core v0.4.0/go.mod h1:lwv0kTB665hTKIR5j4XJcERxQ6j29aECxFUloq2szew=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand Down
16 changes: 16 additions & 0 deletions models/model.go → models/models.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
// Package models defines the different object models used in User
package models

type contextKey string

func (c contextKey) String() string {
return string(c)
}

var (
// ContextKeyParsedToken var
ContextKeyParsedToken = contextKey("ParsedToken")
)

// ParsedToken contains details that are encoded in the received JWT token
type ParsedToken struct {
Email string
}

// User defines the user object
type User struct {
}
Expand Down
11 changes: 6 additions & 5 deletions services/transport/grpc/authorization-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"context"

userGRPCContract "github.com/decentralized-cloud/user/contract/grpc/go"
"github.com/decentralized-cloud/user/models"
"github.com/go-kit/kit/endpoint"
"github.com/lestrrat-go/jwx/jwt"
"github.com/micro-business/go-core/pkg/util"
"github.com/micro-business/go-core/jwt/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand All @@ -21,13 +22,10 @@ var authorizedFuncs = map[string]authorizeFunc{
"DeleteUser": isAuthorizedToCallDeleteUser,
}

// CreateLoggingMiddleware creates the logging middleware.
// endpointName: Mandatory. The name of the endpoint
// Returns the new endpoint with logging middleware added
func (service *transportService) createAuthMiddleware(endpointName string) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
token, err := util.ParseAndVerifyToken(ctx, service.jwksURL, true)
token, err := grpc.ParseAndVerifyToken(ctx, service.jwksURL, true)
if err != nil {
return nil, err
}
Expand All @@ -36,6 +34,9 @@ func (service *transportService) createAuthMiddleware(endpointName string) endpo
return nil, err
}

parsedToken := models.ParsedToken{Email: token.PrivateClaims()["email"].(string)}
ctx = context.WithValue(ctx, models.ContextKeyParsedToken, parsedToken)

return next(ctx, request)
}
}
Expand Down
8 changes: 2 additions & 6 deletions services/transport/grpc/encoder-decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
userGRPCContract "github.com/decentralized-cloud/user/contract/grpc/go"
"github.com/decentralized-cloud/user/models"
"github.com/decentralized-cloud/user/services/business"
"github.com/micro-business/go-core/pkg/util"
commonErrors "github.com/micro-business/go-core/system/errors"
)

Expand All @@ -18,13 +17,10 @@ import (
func decodeCreateUserRequest(
ctx context.Context,
request interface{}) (interface{}, error) {
token, err := util.ParseAndVerifyToken(ctx, "", false)
if err != nil {
return nil, err
}
parsedToken := ctx.Value(models.ContextKeyParsedToken).(models.ParsedToken)

return &business.CreateUserRequest{
Email: token.PrivateClaims()["email"].(string),
Email: parsedToken.Email,
User: models.User{}}, nil
}

Expand Down

0 comments on commit 40521b5

Please sign in to comment.