Ejemplo n.º 1
0
        public IDisposable BeginScope()
        {
            var scope = new ScopeCache();

            GetCurrentScopes().Push(scope);
            return(new EndScope(this, scope));
        }
Ejemplo n.º 2
0
 internal static string GetValidationMessageElement(
     IDictionary <object, object> scope = null,
     HttpContextBase httpContext        = null
     )
 {
     return(ScopeCache.Get(scope, httpContext).ValidationMessageElement);
 }
Ejemplo n.º 3
0
 internal static bool GetUnobtrusiveJavaScriptEnabled(
     IDictionary <object, object> scope = null,
     HttpContextBase httpContext        = null
     )
 {
     return(ScopeCache.Get(scope, httpContext).UnobtrusiveJavaScriptEnabled);
 }
Ejemplo n.º 4
0
 internal static bool GetClientValidationEnabled(
     IDictionary <object, object> scope = null,
     HttpContextBase httpContext        = null
     )
 {
     return(ScopeCache.Get(scope, httpContext).ClientValidationEnabled);
 }
Ejemplo n.º 5
0
            public static ScopeCache Get(IDictionary <object, object> scope, HttpContextBase httpContext)
            {
                if (httpContext == null && Web.HttpContext.Current != null)
                {
                    httpContext = new HttpContextWrapper(Web.HttpContext.Current);
                }

                ScopeCache result = null;

                scope = scope ?? ScopeStorage.CurrentScope;

                if (httpContext != null)
                {
                    result = httpContext.Items[_cacheKey] as ScopeCache;
                }

                if (result == null || result._scope != scope)
                {
                    result = new ScopeCache(scope);

                    if (httpContext != null)
                    {
                        httpContext.Items[_cacheKey] = result;
                    }
                }

                return(result);
            }
Ejemplo n.º 6
0
 internal static void SetValidationMessageElement(
     string elementName,
     IDictionary <object, object> scope = null,
     HttpContextBase httpContext        = null)
 {
     ScopeCache.Get(scope, httpContext).ValidationMessageElement = elementName;
 }
Ejemplo n.º 7
0
 internal static void SetUnobtrusiveJavaScriptEnabled(
     bool enabled,
     IDictionary <object, object> scope = null,
     HttpContextBase httpContext        = null
     )
 {
     ScopeCache.Get(scope, httpContext).UnobtrusiveJavaScriptEnabled = enabled;
 }
Ejemplo n.º 8
0
 internal static void SetClientValidationEnabled(
     bool enabled,
     IDictionary <object, object> scope = null,
     HttpContextBase httpContext        = null
     )
 {
     ScopeCache.Get(scope, httpContext).ClientValidationEnabled = enabled;
 }
Ejemplo n.º 9
0
        public bool OAuthAccess()
        {
            var app = OAuthAppCache.Get(this._appid);

            if (app == null)
            {
                Alert("无效的应用编号");
                return(false);
            }
            //Tauth_Code daCode = new Tauth_Code();
            var daCode = DaoFactory.Tauth_Code();

            if (!daCode.SelectByAppId_GrantCode(app.Id, this._auth_code))
            {
                Alert("无效的授权码");
                return(false);
            }
            if (daCode.Status == 1)
            {
                Alert("该授权码已被使用,不能重复使用");
                return(false);
            }
            if (daCode.Expire_Time < DateTime.Now)
            {
                Alert("授权码已过期");
                return(false);
            }
            daCode.Status = 1;
            if (!daCode.Update())
            {
                Alert("授权码验证失败");
                return(false);
            }
            var scope = ScopeCache.Get(daCode.Scope_Id);

            BeginTransaction();
            UserTokenProvider utp = new UserTokenProvider(app, daCode.User_Id, daCode, scope.Code);

            utp.ReferenceTransactionFrom(Transaction);
            if (!utp.GenerateUserToken())
            {
                Rollback();
                Alert(utp.PromptInfo);
                return(false);
            }
            this.OAuthUser = utp.OAuthUser;
            if (!UpdateTokenRights(utp.TokenId, utp.Refresh_Timeout, daCode.Right_Json))
            {
                Rollback();
                return(false);
            }
            Commit();
            return(true);
        }
Ejemplo n.º 10
0
        private void EndCurrentScope(ScopeCache scopeCache)
        {
            var scopes = GetCurrentScopes();

            if (scopes.Peek() != scopeCache)
            {
                throw new InvalidOperationException(
                          "The scope is not current.  Did you forget to end a child scope?");
            }

            scopeCache.Dispose();
            scopes.Pop();
        }
        protected override void VisitModuleSyntax(ModuleSyntax pNode)
        {
            _locals = new ScopeCache <LocalReference>();
            _locals.AddScope();
            using (var v = Store.AddValue <string>("RunMethod", null))
            {
                base.VisitModuleSyntax(pNode);

                if (pNode == _mainModule && v.Value == null)
                {
                    CompilerErrors.NoRunMethod(pNode.Span);
                }
            }
            _locals.RemoveScope();
        }
Ejemplo n.º 12
0
 public override Task <GrantResponseDTO> GrantByAccount(GrantByAccountRequestDTO request, ServerCallContext context)
 {
     return(Task.Run(() =>
     {
         var response = new GrantResponseDTO();
         OAuthApp app = OAuthAppCache.Get(request.Appid);
         List <Scope> scope = ScopeCache.Get(request.Scopes.Split(','));
         if (app == null)
         {
             response.RetCode = "0400";
             response.RetMsg = "无效的应用id";
             return response;
         }
         string ip = context.GetHttpContext().Request.Headers["X-FORWARD-IP"];
         LoginProvider login = new LoginProvider(request.Account, request.Password, request.Scopes, LoginType.LOGIN_BY_PASSWORD);
         if (!login.Login(request.Client.Type, request.Client.System, request.Client.DeviceId, ip, request.Client.SessionId, request.Client.Version, app.Id))
         {
             response.RetCode = "0500";
             response.RetMsg = login.PromptInfo.CustomMessage;
             return response;
         }
         CodePrivilege[] privileges = null;
         if (request.Grants != null && request.Grants.Count > 0)
         {
             privileges = new CodePrivilege[request.Grants.Count];
             for (int i = 0; i < request.Grants.Count; i++)
             {
                 privileges[i] = new CodePrivilege {
                     Id = request.Grants[i].Id, Type = request.Grants[i].Type
                 };
             }
         }
         GrantTokenPrivilegeProvider grant = new GrantTokenPrivilegeProvider(app.Appid, login.User.UserId, request.Scopes, request.Client.DeviceId);
         if (!grant.Grant(request.GrantAll, privileges))
         {
             response.RetCode = "0500";
             response.RetMsg = "授权失败,请重试";
             return response;
         }
         response.RetCode = "0000";
         response.RetMsg = "ok";
         response.Data = new GrantResponseDTO.Types.Result
         {
             Code = grant.Auth_Code
         };
         return response;
     }));
 }
Ejemplo n.º 13
0
        private object ResolveImpl(Type type, ScopeCache scopeCache)
        {
            lock (_syncRoot)
            {
                if (Registrations.TryGetValue(type, out var registration))
                {
                    return(registration.Resolve(scopeCache));
                }

                if (_parentContainer != null)
                {
                    return(_parentContainer.ResolveImpl(type, scopeCache));
                }

                throw new InvalidOperationException($"Type is not registered: {type.FullName}");
            }
        }
Ejemplo n.º 14
0
        protected override void VisitMemberAccessSyntax(MemberAccessSyntax pNode)
        {
            Visit(pNode.Identifier);

            //Save current local definitions
            //Mark the current type we are on so error messages can be more descriptive
            var l = _locals;

            using (var t = Store.AddValue("__Type", pNode.Identifier.Type))
            {
                //If field doesn't exist or something went wrong, stop checking things to reduce redundant errors
                if (CurrentType != SmallTypeCache.Undefined)
                {
                    //For methods and arrays we need to allow existing variables, but member access should only allow the struct's fields
                    if (NeedToCopyLocals(pNode.Value))
                    {
                        _locals = _locals.Copy();
                    }
                    else
                    {
                        _locals = new ScopeCache <LocalDefinition>();
                    }

                    //Namespaces return a null type
                    if (CurrentType != null)
                    {
                        _locals.AddScope();
                        foreach (var f in CurrentType.GetFields())
                        {
                            if (!_locals.IsVariableDefinedInScope(f.Name))
                            {
                                _locals.DefineVariableInScope(f.Name, LocalDefinition.Create(false, f.Type));
                            }
                        }
                    }

                    Visit(pNode.Value);
                }
            }

            //Restore local definitions
            Namespace = null;
            _locals   = l;
        }
Ejemplo n.º 15
0
        [HttpPost] // api/authorize
        public ResponseResult <GrantResponseDTO> GrantByAccount(GrantByAccountRequestDTO data)
        {
            OAuthApp     app   = OAuthAppCache.Get(data.Appid);
            List <Scope> scope = ScopeCache.Get(data.Scopes.Split(','));

            if (app == null)
            {
                return(Fail <GrantResponseDTO>("无效的应用id", "0400"));
            }
            string        ip    = Request.Headers["X-FORWARD-IP"];
            LoginProvider login = new LoginProvider(data.Account, data.Password, data.Scopes, LoginType.LOGIN_BY_PASSWORD);

            if (!login.Login(data.Client.Type, data.Client.System, data.Client.DeviceId, ip, data.Client.SessionId, data.Client.Version, app.Id))
            {
                return(Fail <GrantResponseDTO>(login.PromptInfo.CustomMessage, "0500"));
            }
            CodePrivilege[] privileges = null;
            if (data.Privileges != null && data.Privileges.Count > 0)
            {
                privileges = new CodePrivilege[data.Privileges.Count];
                for (int i = 0; i < data.Privileges.Count; i++)
                {
                    privileges[i] = new CodePrivilege {
                        Id = data.Privileges[i].Id, Type = data.Privileges[i].Type
                    };
                }
            }
            GrantTokenPrivilegeProvider grant = new GrantTokenPrivilegeProvider(app.Appid, login.User.UserId, data.Scopes, data.Client.DeviceId);

            if (!grant.Grant(data.GrantAll, privileges))
            {
                return(Fail <GrantResponseDTO>("授权失败,请重试", "0500"));
            }
            var response = new GrantResponseDTO
            {
                Code = grant.Auth_Code
            };

            return(Success(response));
        }
Ejemplo n.º 16
0
            public object Resolve(ScopeCache scopeCache)
            {
                switch (_lifetime)
                {
                case NSubLifetime.Transient:
                    return(_factory.Invoke(scopeCache));

                case NSubLifetime.Singleton:
                    return(_singletonValue ?? (_singletonValue = _factory.Invoke(scopeCache)));

                case NSubLifetime.PerScope:
                    if (scopeCache.TryGetValue(this, out var result))
                    {
                        return(result);
                    }

                    result = _factory.Invoke(scopeCache);
                    scopeCache.Set(this, result);
                    return(result);

                default:
                    throw new InvalidOperationException("Unknown lifetime.");
                }
            }
Ejemplo n.º 17
0
        /// <summary>
        /// 执行授权
        /// </summary>
        /// <returns></returns>
        public bool Grant(bool takeAll, params CodePrivilege[] rights)
        {
            OAuthApp app = OAuthAppCache.Get(this._appid);

            if (app == null)
            {
                Alert("未注册的应用");
                return(false);
            }
            string[] scopeCodes = this._scope.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            var      scope      = ScopeCache.Get(scopeCodes);

            if (scope == null || scope.Count <= 0)
            {
                Alert("未定义的授权类型");
                return(false);
            }
            var   fac  = UserModuleFactory.GetUserModuleInstance();
            IUser user = fac?.GetUserByID(this._userId);

            if (user == null)
            {
                Alert("用户信息加载失败");
                return(false);
            }
            if (CheckAlreadyAuth(app.Id, user.UserId))
            {
                return(true);
            }
            if (takeAll && (rights == null || rights.Length <= 0))
            {
                var temp = ScopeRightProvider.GetScopeRights(this._scope);
                rights = new CodePrivilege[temp.Count];
                for (int i = 0; i < rights.Length; i++)
                {
                    rights[i] = new CodePrivilege
                    {
                        Id   = temp[i].Right_Id,
                        Type = temp[i].Right_Type
                    };
                }
            }
            this.Auth_Code = Guid.NewGuid().ToString("N");
            //Tauth_Code daCode = new Tauth_Code();
            var daCode = DaoFactory.Tauth_Code();

            daCode.App_Id      = app.Id;
            daCode.Expire_Time = DateTime.Now.AddMinutes(5);
            daCode.Grant_Code  = this.Auth_Code;
            daCode.Scope_Id    = scope.FirstOrDefault().Id;
            daCode.User_Id     = user.UserId;
            daCode.Device_Id   = this._device_id;
            if (rights != null && rights.Length > 0)
            {
                daCode.Right_Json = Javirs.Common.Json.JsonSerializer.JsonSerialize(rights);
            }
            if (!daCode.Insert())
            {
                Alert("授权失败,请重试!");
                return(false);
            }
            return(true);
        }
Ejemplo n.º 18
0
 /// <summary>
 /// 生成用户授权访问令牌
 /// </summary>
 /// <returns></returns>
 public bool GenerateUserToken()
 {
     try
     {
         if (_app == null && !_appid.HasValue)
         {
             Alert(Winner.Framework.Utils.ResultType.无效数据类型, "无效的应用编号");
             return(false);
         }
         if (_app == null)
         {
             _app = OAuthAppCache.Get(_appid.Value);
         }
         this.OAuthUser.Expire_In         = _app.TokenExpireIn;
         this.OAuthUser.Refresh_Expire_In = 30;
         var   fac  = UserModuleFactory.GetUserModuleInstance();
         IUser user = fac?.GetUserByID(_userid);
         if (user == null)
         {
             Alert(Winner.Framework.Utils.ResultType.数据库查不到数据, "用户不存在");
             return(false);
         }
         if (_daCode == null)
         {
             //_daCode = new Tauth_Code();
             _daCode = DaoFactory.Tauth_Code();
             if (this._authid.HasValue)
             {
                 if (!_daCode.SelectByPk(this._authid.Value))
                 {
                     Alert(Winner.Framework.Utils.ResultType.无效数据类型, "无效的授权码");
                     return(false);
                 }
             }
             else
             {
                 _daCode.App_Id      = _app.Id;
                 _daCode.Expire_Time = DateTime.Now.AddMinutes(5);
                 _daCode.Grant_Code  = Guid.NewGuid().ToString("N").ToLower();
                 _daCode.Scope_Id    = ScopeCache.Get(this._scope).Id;
                 _daCode.User_Id     = user.UserId;
                 _daCode.Device_Id   = _deviceid;
                 _daCode.Remarks     = "客户端登录自动授权";
                 _daCode.Status      = 1;
                 if (!_daCode.Insert())
                 {
                     Alert(Winner.Framework.Utils.ResultType.非法操作, "登录授权失败");
                     return(false);
                 }
             }
         }
         int    refresh_token_expire_in = this.OAuthUser.Refresh_Expire_In * 86400;
         string userCode = user.GetUserVoucher(UserVoucherType.自定义号码);
         string open_id  = EncryptOpenId(_app.Id, user.UserId, userCode, _app.UidEncryptKey);
         this.OAuthUser.Open_Id       = open_id;
         this.OAuthUser.Token         = EncryptAccessToken(user.UserId, userCode, _app.Id, this.OAuthUser.Expire_In);
         this.OAuthUser.Refresh_Token = EncryptAccessToken(user.UserId, userCode, _app.Id, refresh_token_expire_in);
         BeginTransaction();
         //Tauth_Token daToken = new Tauth_Token();
         var daToken = DaoFactory.Tauth_Token();
         daToken.ReferenceTransactionFrom(Transaction);
         bool exist = daToken.SelectByAppId_UserId_DeviceId(_app.Id, this._userid, this._deviceid);
         daToken.App_Id          = _app.Id;
         daToken.Expire_Time     = DateTime.Now.AddSeconds(this.OAuthUser.Expire_In);
         daToken.Refresh_Timeout = DateTime.Now.AddDays(this.OAuthUser.Refresh_Expire_In);
         daToken.Refresh_Token   = this.OAuthUser.Refresh_Token;
         daToken.Token_Code      = this.OAuthUser.Token;
         daToken.Scope_Id        = _daCode.Scope_Id;
         daToken.User_Id         = _userid;
         daToken.Grant_Id        = _daCode.Auth_Id;
         daToken.Device_Id       = this._deviceid;
         if (exist)
         {
             daToken.Last_Access_Time = DateTime.Now;
             if (!daToken.Update())
             {
                 Rollback();
                 Alert(Winner.Framework.Utils.ResultType.数据库更新失败, "TOKEN生成失败");
                 return(false);
             }
         }
         else
         {
             if (!daToken.Insert())
             {
                 Rollback();
                 Alert(Winner.Framework.Utils.ResultType.数据库更新失败, "TOKEN生成失败");
                 return(false);
             }
         }
         this.TokenId = daToken.Token_Id;
         Commit();
         return(true);
     }
     catch (Exception ex)
     {
         Log.Error("生成token失败", ex);
         Alert(ResultType.系统异常, "生成token失败");
         return(false);
     }
 }
Ejemplo n.º 19
0
 public EndScope(ScopingSubsystem manager, ScopeCache scopeCache)
 {
     this.manager    = manager;
     this.scopeCache = scopeCache;
 }
Ejemplo n.º 20
0
 public TypeInferenceVisitor(Compiler.CompilationCache pUnit)
 {
     _locals = new ScopeCache <LocalDefinition>();
     _unit   = pUnit;
 }
Ejemplo n.º 21
0
 public ScopeCacheBoundResolver(NSubContainer container, ScopeCache scopeCache)
 {
     _container  = container;
     _scopeCache = scopeCache;
 }
 public MicrodotServiceProviderWithScope2(IResolutionRoot resolver)
 {
     _cacheItem = new ScopeCache();
     _microdotNinectScopParameter = new MicrodotNinjectScopeParameter(_cacheItem, this);
     _resolver = resolver;
 }