Exemple #1
0
        public static List <ClientAuthorization> GetUserAuthorizationsForClient(string loginId, string clientKey, bool includeInactive)
        {
            using (OAuthDataContext context = OAuthContextHelper.GetContext())
            {
                Guid clientKeyGuid = Guid.Empty;

                Guid.TryParse(clientKey, out clientKeyGuid);
                var query = context.AuthorizationDatas
                            .Where(a => a.login_id == loginId)
                            .Where(a => a.ClientData.api_key == clientKeyGuid);

                if (!includeInactive)
                {
                    query = query.Where(a => a.active);
                }

                var clientAuth = query.Select(a => new ClientAuthorization()
                {
                    AuthorizationId  = a.authorization_id,
                    ClientId         = a.client_id,
                    ScopeId          = a.scope_id,
                    LoginId          = a.login_id,
                    ScopeIdentifier  = a.ScopeData.scope_identifier,
                    ScopeDescription = a.ScopeData.scope_description,
                    Active           = a.active
                }).ToList();

                return(clientAuth);
            }
        }
Exemple #2
0
        public bool Save()
        {
            using (OAuthDataContext context = OAuthContextHelper.GetContext())
            {
                AuthorizationData data;

                if (AuthorizationId > 0)
                {
                    data = context.AuthorizationDatas.FirstOrDefault(a => a.authorization_id == AuthorizationId);
                }
                else
                {
                    data = new AuthorizationData();
                    data.date_created = DateTime.Now;
                }

                data.client_id = ClientId;
                data.login_id  = LoginId;
                data.scope_id  = ScopeId;
                data.active    = Active;

                if (AuthorizationId == 0)
                {
                    context.AuthorizationDatas.InsertOnSubmit(data);
                }

                context.SubmitChanges();

                Load(data);

                return(AuthorizationId > 0);
            }
        }
Exemple #3
0
 private void Load(string identifier)
 {
     using (OAuthDataContext context = OAuthContextHelper.GetContext())
     {
         var data = context.ScopeDatas.FirstOrDefault(s => s.scope_identifier.ToLower() == identifier.ToLower());
         Load(data);
     }
 }
Exemple #4
0
 public static List <Scope> LoadScopes()
 {
     using (OAuthDataContext context = OAuthContextHelper.GetContext())
     {
         List <Scope> scopes = context.ScopeDatas.Select(s => new Scope(s)).ToList();
         return(scopes);
     }
 }
Exemple #5
0
 public static List <int> GetOldPersonIds(int personId)
 {
     using (OAuthDataContext context = OAuthContextHelper.GetContext())
     {
         return(context.PersonMergedDatas.Where(p => p.new_person_id == personId)
                .Select(p => p.old_person_id).ToList());
     }
 }
Exemple #6
0
        private void Load(int id)
        {
            using (OAuthDataContext context = OAuthContextHelper.GetContext())
            {
                var data = context.ScopeDatas.FirstOrDefault(s => s.scope_id == id);

                Load(data);
            }
        }
Exemple #7
0
        private void Load(int authId)
        {
            using (OAuthDataContext context = OAuthContextHelper.GetContext())
            {
                var auth = context.AuthorizationDatas.FirstOrDefault(a => a.authorization_id == authId);

                Load(auth);
            }
        }
Exemple #8
0
        private void Load(Guid apiKey)
        {
            using (OAuthDataContext context = OAuthContextHelper.GetContext())
            {
                var client = context.ClientDatas.FirstOrDefault(c => c.api_key == apiKey);

                Load(client);
            }
        }
Exemple #9
0
        private void Load(int clientId)
        {
            using (OAuthDataContext context = OAuthContextHelper.GetContext())
            {
                var client = context.ClientDatas.FirstOrDefault(c => c.client_id == clientId);

                Load(client);
            }
        }
Exemple #10
0
        public bool Save(string userId)
        {
            try
            {
                using (OAuthDataContext context = OAuthContextHelper.GetContext())
                {
                    ScopeData data;

                    if (ScopeId > 0)
                    {
                        data = context.ScopeDatas.FirstOrDefault(s => s.scope_id == ScopeId);

                        if (data == null)
                        {
                            throw new ArgumentException("Scope Id is not valid.", "ScopeId");
                        }
                    }
                    else
                    {
                        if (ScopeIdentifierInUse())
                        {
                            throw new ArgumentException("Scope Identifier is already in use.", "ScopeIdentifier");
                        }

                        data              = new ScopeData();
                        data.created_by   = userId;
                        data.date_created = DateTime.Now;
                    }

                    data.scope_identifier  = Identifier;
                    data.scope_description = Description;
                    data.modified_by       = userId;
                    data.date_modified     = DateTime.Now;
                    data.active            = Active;

                    if (ScopeId <= 0)
                    {
                        context.ScopeDatas.InsertOnSubmit(data);
                    }

                    context.SubmitChanges();

                    if (data.scope_id > 0)
                    {
                        Load(data);
                        return(true);
                    }

                    return(false);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("An error occurred while saving Scope", ex);
            }
        }
Exemple #11
0
        public static List <Scope> GetScopesByName(string identifier)
        {
            using (OAuthDataContext context = OAuthContextHelper.GetContext())
            {
                List <Scope> scopes = context.ScopeDatas
                                      .Where(s => s.scope_identifier.ToLower() == identifier.ToLower())
                                      .Select(s => new Scope(s)).ToList();

                return(scopes);
            }
        }
Exemple #12
0
        public static List <Client> LoadClients(bool includeInactive)
        {
            using (OAuthDataContext context = OAuthContextHelper.GetContext())
            {
                var query = context.ClientDatas;

                if (!includeInactive)
                {
                    query.Where(q => q.active);
                }

                return(query.Select(q => new Client(q)).ToList());
            }
        }
        private void PersistAuthenticationTokens()
        {
            using (var context = new OAuthDataContext())
            {
                context.Database.ExecuteSqlCommand("DELETE FROM [OAuthAuthentications]");

                var item = new OAuthAuthentication();
                item.AccessToken   = _accessToken;
                item.RefreshToken  = _refreshToken;
                item.Expiry        = _tokenExpiry;
                item.AuthorizeCode = _authorizeCode;
                context.OAuthAuthentication.Add(item);
                context.SaveChanges();
            }
        }
Exemple #14
0
        private void LoadScopes()
        {
            mScopes = null;
            using (OAuthDataContext context = OAuthContextHelper.GetContext())
            {
                var scopeDatas = context.ClientDatas
                                 .Where(c => c.client_id == ClientId)
                                 .SelectMany(c => c.ClientScopeDatas.Select(cs => cs.ScopeData))
                                 .ToList();

                if (scopeDatas != null)
                {
                    mScopes = scopeDatas.Select(s => new Scope(s)).ToList();
                }
            }
        }
        private void LoadAuthenticationTokens()
        {
            using (var context = new OAuthDataContext())
            {
                var auth = context.OAuthAuthentication.FirstOrDefault();
                if (auth == null)
                {
                    return;
                }

                _accessToken   = auth.AccessToken;
                _refreshToken  = auth.RefreshToken;
                _tokenExpiry   = auth.Expiry;
                _authorizeCode = auth.AuthorizeCode;
                context.SaveChanges();
            }
        }
Exemple #16
0
        public static void AuthorizeScopes(string loginId, string clientKey, string[] scopes)
        {
            Guid clientKeyGuid;

            if (!Guid.TryParse(clientKey, out clientKeyGuid))
            {
                throw new ArgumentException("Client Key/ID is not valid", "clientKey");
            }

            Client c = new Client(clientKeyGuid);

            if (c.ClientId <= 0)
            {
                throw new ArgumentException("Client Key/ID is not valid.", "clientKey");
            }

            foreach (var scope in scopes)
            {
                using (OAuthDataContext context = OAuthContextHelper.GetContext())
                {
                    bool isExistingAuth = context.AuthorizationDatas
                                          .Where(a => a.client_id == c.ClientId)
                                          .Where(a => a.login_id.ToLower() == loginId.ToLower())
                                          .Where(a => a.ScopeData.scope_identifier.ToLower() == scope.ToLower())
                                          .Where(a => a.active)
                                          .Count() > 0;

                    bool isClientScope = c.Scopes.Where(s => s.Identifier.ToLower() == scope.ToLower())
                                         .Where(s => s.Active).Count() > 0;

                    if (isClientScope && !isExistingAuth)
                    {
                        AddUserAuthorization(loginId, c.ClientId, new Scope(scope).ScopeId);
                    }
                    else if (!isClientScope)
                    {
                        throw new ArgumentException(string.Format("Scope {0} is not valid for client.", scope), "scope");
                    }
                }
            }
        }
Exemple #17
0
        private void LoadClients()
        {
            using (OAuthDataContext context = OAuthContextHelper.GetContext())
            {
                //List<ClientData> clientData = context.ScopeDatas
                //                  .FirstOrDefault( s => s.scope_id == ScopeId )
                //                  .ClientScopeDatas.Select( s => s.ClientData ).ToList();

                //mClients = clientData.Select( c => new Client( c ) ).ToList();

                var clientData = context.ScopeDatas
                                 .Where(s => s.scope_id == ScopeId)
                                 .SelectMany(s => s.ClientScopeDatas.Select(cs => cs.ClientData))
                                 .ToList();

                if (clientData != null)
                {
                    mClients = clientData.Select(c => new Client(c)).ToList();
                }
            }
        }
Exemple #18
0
        public bool Save(string userId)
        {
            try
            {
                using (OAuthDataContext context = OAuthContextHelper.GetContext())
                {
                    ClientData data;

                    if (ClientId > 0)
                    {
                        data = context.ClientDatas.FirstOrDefault(c => c.client_id == ClientId);
                        if (data == null)
                        {
                            throw new ArgumentOutOfRangeException("Client Not Found for ClientID");
                        }
                    }
                    else
                    {
                        data = new ClientData();

                        if (ApiKey == Guid.Empty)
                        {
                            GenerateApiKey();
                        }
                        if (ApiSecret == Guid.Empty)
                        {
                            GenerateApiSecret();
                        }
                    }

                    data.client_name   = Name;
                    data.callback_url  = Callback;
                    data.api_key       = ApiKey;
                    data.api_secret    = ApiSecret;
                    data.modified_by   = userId;
                    data.date_modified = DateTime.Now;
                    data.active        = Active;

                    if (data.client_id <= 0)
                    {
                        data.created_by   = userId;
                        data.date_created = DateTime.Now;

                        if (Scopes != null)
                        {
                            foreach (var scope in Scopes)
                            {
                                data.ClientScopeDatas.Add(new ClientScopeData
                                {
                                    scope_id      = scope.ScopeId,
                                    created_by    = userId,
                                    date_created  = DateTime.Now,
                                    modified_by   = userId,
                                    date_modified = DateTime.Now,
                                    active        = true
                                });
                            }
                        }


                        context.ClientDatas.InsertOnSubmit(data);
                    }
                    else
                    {
                        var scopeIDRemove = data.ClientScopeDatas.Select(cs => cs.scope_id).Where(cs => !Scopes.Select(s => s.ScopeId).Contains(cs)).ToArray();
                        foreach (var scopeId in scopeIDRemove)
                        {
                            var scopeToRemove = data.ClientScopeDatas.Where(cs => cs.scope_id == scopeId).FirstOrDefault();

                            if (scopeToRemove != null)
                            {
                                context.ClientScopeDatas.DeleteOnSubmit(scopeToRemove);
                            }
                        }

                        var scopeIdAdd = Scopes.Select(s => s.ScopeId).Where(s => !data.ClientScopeDatas.Select(cs => cs.scope_id).Contains(s));


                        foreach (var scopeId in scopeIdAdd)
                        {
                            data.ClientScopeDatas.Add(
                                new ClientScopeData
                            {
                                scope_id      = scopeId,
                                created_by    = userId,
                                modified_by   = userId,
                                date_created  = DateTime.Now,
                                date_modified = DateTime.Now,
                                active        = true
                            }
                                );
                        }
                    }

                    context.SubmitChanges();

                    if (data.client_id > 0)
                    {
                        Load(data);
                        return(true);
                    }

                    return(false);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("An error occurred when saving client.", ex);
            }
        }