コード例 #1
0
        public Task Invoke(RemoteCallerContext context)
        {
            // get jwt token
            if (!string.IsNullOrEmpty(_options.TokenEndpointPath) &&
                context.ServiceEntry == null &&
                context.RemoteInvokeMessage.ServiceId == _options.GetServiceId())
            {
                return(CreateToken(context));
            }
            // jwt authentication, alse authentication the role

            if (context.ServiceEntry != null && context.ServiceEntry.Descriptor.EnableAuthorization)
            {
                return(InvokeService(context));
            }
            // service can be annoymouse request
            return(_next(context));
        }
コード例 #2
0
ファイル: JwtAuthServerModule.cs プロジェクト: yuyu2you/jimu
        public override void DoInit(IContainer container)
        {
            if (_options != null)
            {
                var logger = container.Resolve <ILogger>();
                logger.Info($"[config]use jose.jwt for Auth");

                //while (!container.IsRegistered<IServer>() || !container.IsRegistered<IServiceDiscovery>())
                //{
                //    Thread.Sleep(200);
                //}
                var server = container.Resolve <IServer>();
                server.UseMiddleware <JwtAuthorizationMiddleware>(_options, container);

                if (string.IsNullOrEmpty(_options.TokenEndpointPath))
                {
                    return;
                }
                var discovery  = container.Resolve <IServiceDiscovery>();
                var addr       = new JimuAddress(_options.ServiceInvokeIp, Convert.ToInt32(_options.ServiceInvokePort), _options.Protocol);
                var tokenRoute =
                    new JimuServiceRoute
                {
                    Address = new List <JimuAddress> {
                        addr
                    },
                    ServiceDescriptor = new JimuServiceDesc
                    {
                        Id         = _options.GetServiceId(),
                        Service    = "Token",
                        RoutePath  = JimuServiceRoute.ParseRoutePath("", "", _options.TokenEndpointPath, new[] { "username", "password" }, false),
                        Parameters = JimuHelper.Serialize <string>(new List <JimuServiceParameterDesc> {
                            new JimuServiceParameterDesc
                            {
                                Comment = "username",
                                Format  = "System.String",
                                Name    = "username",
                                Type    = "object"
                            },
                            new JimuServiceParameterDesc
                            {
                                Comment = "password",
                                Format  = "System.String",
                                Name    = "password",
                                Type    = "object"
                            },
                        }),
                        ReturnDesc = JimuHelper.Serialize <string>(new JimuServiceReturnDesc
                        {
                            Comment      = "Token",
                            ReturnType   = "object",
                            ReturnFormat = "{\"access_token\":\"System.String | token\", \"expired_in\":\"System.Int32 | expired timestamp which is the number of seconds between 1970-01-01 and expired datetime\"}"
                        })
                    }
                };
                //discovery.ClearServiceAsync(tokenRoute.First().ServiceDescriptor.Id).Wait();
                ////discovery.SetRoutesAsync(tokenRoute);
                //discovery.AddRouteAsync(tokenRoute).Wait();
                discovery.OnBeforeSetRoutes += (routes) =>
                {
                    routes.Add(tokenRoute);
                };
            }

            base.DoInit(container);
        }