github.com/cs3org/reva/v2@v2.27.7/pkg/auth/rpc_auth.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package auth
    20  
    21  import (
    22  	"context"
    23  	"net/rpc"
    24  
    25  	authpb "github.com/cs3org/go-cs3apis/cs3/auth/provider/v1beta1"
    26  	user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
    27  	"github.com/cs3org/reva/v2/pkg/appctx"
    28  	"github.com/cs3org/reva/v2/pkg/plugin"
    29  	hcplugin "github.com/hashicorp/go-plugin"
    30  )
    31  
    32  func init() {
    33  	plugin.Register("authprovider", &ProviderPlugin{})
    34  }
    35  
    36  // ProviderPlugin is the implementation of plugin.Plugin so we can serve/consume this.
    37  type ProviderPlugin struct {
    38  	Impl Manager
    39  }
    40  
    41  // Server returns the RPC Server which serves the methods that the Client calls over net/rpc
    42  func (p *ProviderPlugin) Server(*hcplugin.MuxBroker) (interface{}, error) {
    43  	return &RPCServer{Impl: p.Impl}, nil
    44  }
    45  
    46  // Client returns interface implementation for the plugin that communicates to the server end of the plugin
    47  func (p *ProviderPlugin) Client(b *hcplugin.MuxBroker, c *rpc.Client) (interface{}, error) {
    48  	return &RPCClient{Client: c}, nil
    49  }
    50  
    51  // RPCClient is an implementation of Manager that talks over RPC.
    52  type RPCClient struct{ Client *rpc.Client }
    53  
    54  // ConfigureArg for RPC
    55  type ConfigureArg struct {
    56  	Ml map[string]interface{}
    57  }
    58  
    59  // ConfigureReply for RPC
    60  type ConfigureReply struct {
    61  	Err error
    62  }
    63  
    64  // Configure RPCClient configure method
    65  func (m *RPCClient) Configure(ml map[string]interface{}) error {
    66  	args := ConfigureArg{Ml: ml}
    67  	resp := ConfigureReply{}
    68  	err := m.Client.Call("Plugin.Configure", args, &resp)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	return resp.Err
    73  }
    74  
    75  // AuthenticateArgs for RPC
    76  type AuthenticateArgs struct {
    77  	Ctx          map[interface{}]interface{}
    78  	ClientID     string
    79  	ClientSecret string
    80  }
    81  
    82  // AuthenticateReply for RPC
    83  type AuthenticateReply struct {
    84  	User  *user.User
    85  	Auth  map[string]*authpb.Scope
    86  	Error error
    87  }
    88  
    89  // Authenticate RPCClient Authenticate method
    90  func (m *RPCClient) Authenticate(ctx context.Context, clientID, clientSecret string) (*user.User, map[string]*authpb.Scope, error) {
    91  	ctxVal := appctx.GetKeyValuesFromCtx(ctx)
    92  	args := AuthenticateArgs{Ctx: ctxVal, ClientID: clientID, ClientSecret: clientSecret}
    93  	reply := AuthenticateReply{}
    94  	err := m.Client.Call("Plugin.Authenticate", args, &reply)
    95  	if err != nil {
    96  		return nil, nil, err
    97  	}
    98  	return reply.User, reply.Auth, reply.Error
    99  }
   100  
   101  // RPCServer is the server that RPCClient talks to, conforming to the requirements of net/rpc
   102  type RPCServer struct {
   103  	// This is the real implementation
   104  	Impl Manager
   105  }
   106  
   107  // Configure RPCServer Configure method
   108  func (m *RPCServer) Configure(args ConfigureArg, resp *ConfigureReply) error {
   109  	resp.Err = m.Impl.Configure(args.Ml)
   110  	return nil
   111  }
   112  
   113  // Authenticate RPCServer Authenticate method
   114  func (m *RPCServer) Authenticate(args AuthenticateArgs, resp *AuthenticateReply) error {
   115  	ctx := appctx.PutKeyValuesToCtx(args.Ctx)
   116  	resp.User, resp.Auth, resp.Error = m.Impl.Authenticate(ctx, args.ClientID, args.ClientSecret)
   117  	return nil
   118  }