From 1838e4a1ee0b64b49dd67682ddd6168e935e78cd Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Sun, 19 Jul 2020 11:37:40 +0100 Subject: [PATCH] remove auth provider --- auth/default.go | 5 +- auth/options.go | 10 -- auth/provider/basic/basic.go | 34 ------ auth/provider/oauth/oauth.go | 65 ------------ auth/provider/options.go | 47 --------- auth/provider/provider.go | 49 --------- cmd/cmd.go | 64 ------------ errors/proto/errors.pb.go | 176 ++++++++++++++++++++++++++++++++ errors/proto/errors.pb.micro.go | 21 ++++ errors/proto/errors.proto | 10 ++ go.mod | 2 +- server/grpc/grpc.go | 18 +++- 12 files changed, 225 insertions(+), 276 deletions(-) delete mode 100644 auth/provider/basic/basic.go delete mode 100644 auth/provider/oauth/oauth.go delete mode 100644 auth/provider/options.go delete mode 100644 auth/provider/provider.go create mode 100644 errors/proto/errors.pb.go create mode 100644 errors/proto/errors.pb.micro.go create mode 100644 errors/proto/errors.proto diff --git a/auth/default.go b/auth/default.go index 0b77aec0..02614936 100644 --- a/auth/default.go +++ b/auth/default.go @@ -2,7 +2,6 @@ package auth import ( "github.com/google/uuid" - "github.com/micro/go-micro/v2/auth/provider/basic" ) var ( @@ -10,9 +9,7 @@ var ( ) func NewAuth(opts ...Option) Auth { - options := Options{ - Provider: basic.NewProvider(), - } + var options Options for _, o := range opts { o(&options) diff --git a/auth/options.go b/auth/options.go index be3354f8..4c06b854 100644 --- a/auth/options.go +++ b/auth/options.go @@ -4,7 +4,6 @@ import ( "context" "time" - "github.com/micro/go-micro/v2/auth/provider" "github.com/micro/go-micro/v2/client" "github.com/micro/go-micro/v2/store" ) @@ -34,8 +33,6 @@ type Options struct { PublicKey string // PrivateKey for encoding JWTs PrivateKey string - // Provider is an auth provider - Provider provider.Provider // LoginURL is the relative url path where a user can login LoginURL string // Store to back auth @@ -98,13 +95,6 @@ func ClientToken(token *Token) Option { } } -// Provider set the auth provider -func Provider(p provider.Provider) Option { - return func(o *Options) { - o.Provider = p - } -} - // LoginURL sets the auth LoginURL func LoginURL(url string) Option { return func(o *Options) { diff --git a/auth/provider/basic/basic.go b/auth/provider/basic/basic.go deleted file mode 100644 index ed19190a..00000000 --- a/auth/provider/basic/basic.go +++ /dev/null @@ -1,34 +0,0 @@ -package basic - -import ( - "github.com/micro/go-micro/v2/auth/provider" -) - -// NewProvider returns an initialised basic provider -func NewProvider(opts ...provider.Option) provider.Provider { - var options provider.Options - for _, o := range opts { - o(&options) - } - return &basic{options} -} - -type basic struct { - opts provider.Options -} - -func (b *basic) String() string { - return "basic" -} - -func (b *basic) Options() provider.Options { - return b.opts -} - -func (b *basic) Endpoint(...provider.EndpointOption) string { - return "" -} - -func (b *basic) Redirect() string { - return "" -} diff --git a/auth/provider/oauth/oauth.go b/auth/provider/oauth/oauth.go deleted file mode 100644 index d04d7cf7..00000000 --- a/auth/provider/oauth/oauth.go +++ /dev/null @@ -1,65 +0,0 @@ -package oauth - -import ( - "fmt" - "net/url" - - "github.com/micro/go-micro/v2/auth/provider" -) - -// NewProvider returns an initialised oauth provider -func NewProvider(opts ...provider.Option) provider.Provider { - var options provider.Options - for _, o := range opts { - o(&options) - } - return &oauth{options} -} - -type oauth struct { - opts provider.Options -} - -func (o *oauth) String() string { - return "oauth" -} - -func (o *oauth) Options() provider.Options { - return o.opts -} - -func (o *oauth) Endpoint(opts ...provider.EndpointOption) string { - var options provider.EndpointOptions - for _, o := range opts { - o(&options) - } - - params := make(url.Values) - params.Add("response_type", "code") - - if len(options.State) > 0 { - params.Add("state", options.State) - } - - if len(options.LoginHint) > 0 { - params.Add("login_hint", options.LoginHint) - } - - if clientID := o.opts.ClientID; len(clientID) > 0 { - params.Add("client_id", clientID) - } - - if scope := o.opts.Scope; len(scope) > 0 { - params.Add("scope", scope) - } - - if redir := o.Redirect(); len(redir) > 0 { - params.Add("redirect_uri", redir) - } - - return fmt.Sprintf("%v?%v", o.opts.Endpoint, params.Encode()) -} - -func (o *oauth) Redirect() string { - return o.opts.Redirect -} diff --git a/auth/provider/options.go b/auth/provider/options.go deleted file mode 100644 index 930df479..00000000 --- a/auth/provider/options.go +++ /dev/null @@ -1,47 +0,0 @@ -package provider - -// Option returns a function which sets an option -type Option func(*Options) - -// Options a provider can have -type Options struct { - // ClientID is the application's ID. - ClientID string - // ClientSecret is the application's secret. - ClientSecret string - // Endpoint for the provider - Endpoint string - // Redirect url incase of UI - Redirect string - // Scope of the oauth request - Scope string -} - -// Credentials is an option which sets the client id and secret -func Credentials(id, secret string) Option { - return func(o *Options) { - o.ClientID = id - o.ClientSecret = secret - } -} - -// Endpoint sets the endpoint option -func Endpoint(e string) Option { - return func(o *Options) { - o.Endpoint = e - } -} - -// Redirect sets the Redirect option -func Redirect(r string) Option { - return func(o *Options) { - o.Redirect = r - } -} - -// Scope sets the oauth scope -func Scope(s string) Option { - return func(o *Options) { - o.Scope = s - } -} diff --git a/auth/provider/provider.go b/auth/provider/provider.go deleted file mode 100644 index 09e78bdf..00000000 --- a/auth/provider/provider.go +++ /dev/null @@ -1,49 +0,0 @@ -// Package provider is an external auth provider e.g oauth -package provider - -import ( - "time" -) - -// Provider is an auth provider -type Provider interface { - // String returns the name of the provider - String() string - // Options returns the options of a provider - Options() Options - // Endpoint for the provider - Endpoint(...EndpointOption) string - // Redirect url incase of UI - Redirect() string -} - -// Grant is a granted authorisation -type Grant struct { - // token for reuse - Token string - // Expiry of the token - Expiry time.Time - // Scopes associated with grant - Scopes []string -} - -type EndpointOptions struct { - // State is a code to verify the req - State string - // LoginHint prefils the user id on oauth clients - LoginHint string -} - -type EndpointOption func(*EndpointOptions) - -func WithState(c string) EndpointOption { - return func(o *EndpointOptions) { - o.State = c - } -} - -func WithLoginHint(hint string) EndpointOption { - return func(o *EndpointOptions) { - o.LoginHint = hint - } -} diff --git a/cmd/cmd.go b/cmd/cmd.go index 8ed0d501..133fdd8b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -10,7 +10,6 @@ import ( "time" "github.com/micro/go-micro/v2/auth" - "github.com/micro/go-micro/v2/auth/provider" "github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v2/client" "github.com/micro/go-micro/v2/client/grpc" @@ -85,10 +84,6 @@ import ( // auth jwtAuth "github.com/micro/go-micro/v2/auth/jwt" svcAuth "github.com/micro/go-micro/v2/auth/service" - - // auth providers - "github.com/micro/go-micro/v2/auth/provider/basic" - "github.com/micro/go-micro/v2/auth/provider/oauth" ) type Cmd interface { @@ -333,36 +328,6 @@ var ( EnvVars: []string{"MICRO_AUTH_PRIVATE_KEY"}, Usage: "Private key for JWT auth (base64 encoded PEM)", }, - &cli.StringFlag{ - Name: "auth_provider", - EnvVars: []string{"MICRO_AUTH_PROVIDER"}, - Usage: "Auth provider used to login user", - }, - &cli.StringFlag{ - Name: "auth_provider_client_id", - EnvVars: []string{"MICRO_AUTH_PROVIDER_CLIENT_ID"}, - Usage: "The client id to be used for oauth", - }, - &cli.StringFlag{ - Name: "auth_provider_client_secret", - EnvVars: []string{"MICRO_AUTH_PROVIDER_CLIENT_SECRET"}, - Usage: "The client secret to be used for oauth", - }, - &cli.StringFlag{ - Name: "auth_provider_endpoint", - EnvVars: []string{"MICRO_AUTH_PROVIDER_ENDPOINT"}, - Usage: "The enpoint to be used for oauth", - }, - &cli.StringFlag{ - Name: "auth_provider_redirect", - EnvVars: []string{"MICRO_AUTH_PROVIDER_REDIRECT"}, - Usage: "The redirect to be used for oauth", - }, - &cli.StringFlag{ - Name: "auth_provider_scope", - EnvVars: []string{"MICRO_AUTH_PROVIDER_SCOPE"}, - Usage: "The scope to be used for oauth", - }, &cli.StringFlag{ Name: "config", EnvVars: []string{"MICRO_CONFIG"}, @@ -442,11 +407,6 @@ var ( "jwt": jwtAuth.NewAuth, } - DefaultAuthProviders = map[string]func(...provider.Option) provider.Provider{ - "oauth": oauth.NewProvider, - "basic": basic.NewProvider, - } - DefaultProfiles = map[string]func(...profile.Option) profile.Profile{ "http": http.NewProfile, "pprof": pprof.NewProfile, @@ -629,30 +589,6 @@ func (c *cmd) Before(ctx *cli.Context) error { serverOpts = append(serverOpts, server.Namespace(ns)) authOpts = append(authOpts, auth.Issuer(ns)) } - if name := ctx.String("auth_provider"); len(name) > 0 { - p, ok := DefaultAuthProviders[name] - if !ok { - logger.Fatalf("AuthProvider %s not found", name) - } - - var provOpts []provider.Option - clientID := ctx.String("auth_provider_client_id") - clientSecret := ctx.String("auth_provider_client_secret") - if len(clientID) > 0 || len(clientSecret) > 0 { - provOpts = append(provOpts, provider.Credentials(clientID, clientSecret)) - } - if e := ctx.String("auth_provider_endpoint"); len(e) > 0 { - provOpts = append(provOpts, provider.Endpoint(e)) - } - if r := ctx.String("auth_provider_redirect"); len(r) > 0 { - provOpts = append(provOpts, provider.Redirect(r)) - } - if s := ctx.String("auth_provider_scope"); len(s) > 0 { - provOpts = append(provOpts, provider.Scope(s)) - } - - authOpts = append(authOpts, auth.Provider(p(provOpts...))) - } // Set the auth if name := ctx.String("auth"); len(name) > 0 { diff --git a/errors/proto/errors.pb.go b/errors/proto/errors.pb.go new file mode 100644 index 00000000..f9f8fc37 --- /dev/null +++ b/errors/proto/errors.pb.go @@ -0,0 +1,176 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.22.0 +// protoc v3.11.4 +// source: github.com/micro/go-micro/errors/proto/errors.proto + +package errors + +import ( + proto "github.com/golang/protobuf/proto" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// This is a compile-time assertion that a sufficiently up-to-date version +// of the legacy proto package is being used. +const _ = proto.ProtoPackageIsVersion4 + +type Error struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` + Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"` + Detail string `protobuf:"bytes,3,opt,name=detail,proto3" json:"detail,omitempty"` + Status string `protobuf:"bytes,4,opt,name=status,proto3" json:"status,omitempty"` +} + +func (x *Error) Reset() { + *x = Error{} + if protoimpl.UnsafeEnabled { + mi := &file_github_com_micro_go_micro_errors_proto_errors_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Error) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Error) ProtoMessage() {} + +func (x *Error) ProtoReflect() protoreflect.Message { + mi := &file_github_com_micro_go_micro_errors_proto_errors_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Error.ProtoReflect.Descriptor instead. +func (*Error) Descriptor() ([]byte, []int) { + return file_github_com_micro_go_micro_errors_proto_errors_proto_rawDescGZIP(), []int{0} +} + +func (x *Error) GetId() string { + if x != nil { + return x.Id + } + return "" +} + +func (x *Error) GetCode() int32 { + if x != nil { + return x.Code + } + return 0 +} + +func (x *Error) GetDetail() string { + if x != nil { + return x.Detail + } + return "" +} + +func (x *Error) GetStatus() string { + if x != nil { + return x.Status + } + return "" +} + +var File_github_com_micro_go_micro_errors_proto_errors_proto protoreflect.FileDescriptor + +var file_github_com_micro_go_micro_errors_proto_errors_proto_rawDesc = []byte{ + 0x0a, 0x33, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6d, 0x69, 0x63, + 0x72, 0x6f, 0x2f, 0x67, 0x6f, 0x2d, 0x6d, 0x69, 0x63, 0x72, 0x6f, 0x2f, 0x65, 0x72, 0x72, 0x6f, + 0x72, 0x73, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x22, 0x5b, 0x0a, + 0x05, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x65, + 0x74, 0x61, 0x69, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, 0x65, 0x74, 0x61, + 0x69, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x04, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, +} + +var ( + file_github_com_micro_go_micro_errors_proto_errors_proto_rawDescOnce sync.Once + file_github_com_micro_go_micro_errors_proto_errors_proto_rawDescData = file_github_com_micro_go_micro_errors_proto_errors_proto_rawDesc +) + +func file_github_com_micro_go_micro_errors_proto_errors_proto_rawDescGZIP() []byte { + file_github_com_micro_go_micro_errors_proto_errors_proto_rawDescOnce.Do(func() { + file_github_com_micro_go_micro_errors_proto_errors_proto_rawDescData = protoimpl.X.CompressGZIP(file_github_com_micro_go_micro_errors_proto_errors_proto_rawDescData) + }) + return file_github_com_micro_go_micro_errors_proto_errors_proto_rawDescData +} + +var file_github_com_micro_go_micro_errors_proto_errors_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_github_com_micro_go_micro_errors_proto_errors_proto_goTypes = []interface{}{ + (*Error)(nil), // 0: errors.Error +} +var file_github_com_micro_go_micro_errors_proto_errors_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_github_com_micro_go_micro_errors_proto_errors_proto_init() } +func file_github_com_micro_go_micro_errors_proto_errors_proto_init() { + if File_github_com_micro_go_micro_errors_proto_errors_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_github_com_micro_go_micro_errors_proto_errors_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Error); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_github_com_micro_go_micro_errors_proto_errors_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_github_com_micro_go_micro_errors_proto_errors_proto_goTypes, + DependencyIndexes: file_github_com_micro_go_micro_errors_proto_errors_proto_depIdxs, + MessageInfos: file_github_com_micro_go_micro_errors_proto_errors_proto_msgTypes, + }.Build() + File_github_com_micro_go_micro_errors_proto_errors_proto = out.File + file_github_com_micro_go_micro_errors_proto_errors_proto_rawDesc = nil + file_github_com_micro_go_micro_errors_proto_errors_proto_goTypes = nil + file_github_com_micro_go_micro_errors_proto_errors_proto_depIdxs = nil +} diff --git a/errors/proto/errors.pb.micro.go b/errors/proto/errors.pb.micro.go new file mode 100644 index 00000000..8878a03e --- /dev/null +++ b/errors/proto/errors.pb.micro.go @@ -0,0 +1,21 @@ +// Code generated by protoc-gen-micro. DO NOT EDIT. +// source: github.com/micro/go-micro/errors/proto/errors.proto + +package errors + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package diff --git a/errors/proto/errors.proto b/errors/proto/errors.proto new file mode 100644 index 00000000..e9d51ff3 --- /dev/null +++ b/errors/proto/errors.proto @@ -0,0 +1,10 @@ +syntax = "proto3"; + +package errors; + +message Error { + string id = 1; + int32 code = 2; + string detail = 3; + string status = 4; +}; diff --git a/go.mod b/go.mod index b34c2ef8..1132d034 100644 --- a/go.mod +++ b/go.mod @@ -70,7 +70,7 @@ require ( golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1 google.golang.org/grpc v1.26.0 - google.golang.org/protobuf v1.22.0 // indirect + google.golang.org/protobuf v1.22.0 gopkg.in/telegram-bot-api.v4 v4.6.4 sigs.k8s.io/yaml v1.1.0 // indirect ) diff --git a/server/grpc/grpc.go b/server/grpc/grpc.go index 689cd7d3..fdaec7e7 100644 --- a/server/grpc/grpc.go +++ b/server/grpc/grpc.go @@ -17,6 +17,7 @@ import ( "github.com/golang/protobuf/proto" "github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v2/errors" + pberr "github.com/micro/go-micro/v2/errors/proto" "github.com/micro/go-micro/v2/logger" meta "github.com/micro/go-micro/v2/metadata" "github.com/micro/go-micro/v2/registry" @@ -400,10 +401,17 @@ func (g *grpcServer) processRequest(stream grpc.ServerStream, service *service, var errStatus *status.Status switch verr := appErr.(type) { case *errors.Error: + perr := &pberr.Error{ + Id: verr.Id, + Code: verr.Code, + Detail: verr.Detail, + Status: verr.Status, + } + // micro.Error now proto based and we can attach it to grpc status statusCode = microError(verr) statusDesc = verr.Error() - errStatus, err = status.New(statusCode, statusDesc).WithDetails(verr) + errStatus, err = status.New(statusCode, statusDesc).WithDetails(perr) if err != nil { return err } @@ -472,10 +480,16 @@ func (g *grpcServer) processStream(stream grpc.ServerStream, service *service, m var errStatus *status.Status switch verr := appErr.(type) { case *errors.Error: + perr := &pberr.Error{ + Id: verr.Id, + Code: verr.Code, + Detail: verr.Detail, + Status: verr.Status, + } // micro.Error now proto based and we can attach it to grpc status statusCode = microError(verr) statusDesc = verr.Error() - errStatus, err = status.New(statusCode, statusDesc).WithDetails(verr) + errStatus, err = status.New(statusCode, statusDesc).WithDetails(perr) if err != nil { return err }