internal static XElement FromUser(LMConnect.Key.User user)
 {
     return(new XElement("user",
                         new XElement("username", user.Username),
                         new XElement("email", user.Email),
                         new XElement("databases", user.Databases.Select(FromDatabase)),
                         new XElement("miners", user.Miners.Select(FromMiner))
                         ));
 }
Beispiel #2
0
        /// <summary>
        /// Validates the username and password.
        /// </summary>
        /// <param name="token">The token.</param>
        /// <returns>An identity collection representing the identity in the token</returns>
        public override ReadOnlyCollection <ClaimsIdentity> ValidateToken(SecurityToken token)
        {
            if (token == null)
            {
                throw new ArgumentNullException("token");
            }

            if (base.Configuration == null)
            {
                throw new InvalidOperationException("No Configuration set");
            }

            var unToken = token as UserNameSecurityToken;

            if (unToken == null)
            {
                throw new ArgumentException("SecurityToken is not a UserNameSecurityToken");
            }

            LMConnect.Key.User user = ValidateUserNameCredentialCore(unToken.UserName, unToken.Password);

            if (user == null)
            {
                return(new List <ClaimsIdentity> {
                    new ClaimsIdentity()
                }.AsReadOnly());
            }

            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, unToken.UserName),
                new Claim(ClaimTypes.Role, user.Role),
                new Claim(ClaimTypes.AuthenticationMethod, AuthenticationMethods.Password),
                new Claim(ClaimTypes.AuthenticationInstant, XmlConvert.ToString(DateTime.UtcNow, "yyyy-MM-ddTHH:mm:ss.fffZ"), ClaimValueTypes.DateTime)
            };

            var identity = new ClaimsIdentity(claims, "Basic");

            if (Configuration.SaveBootstrapContext)
            {
                if (this.RetainPassword)
                {
                    identity.BootstrapContext = new BootstrapContext(unToken, this);
                }
                else
                {
                    var bootstrapToken = new UserNameSecurityToken(unToken.UserName, null);
                    identity.BootstrapContext = new BootstrapContext(bootstrapToken, this);
                }
            }

            return(new List <ClaimsIdentity> {
                identity
            }.AsReadOnly());
        }
Beispiel #3
0
 public LMConnect.Key.UserPendingUpdate GetPendingUpdate(LMConnect.Key.User user)
 {
     return(new LMConnect.Key.UserPendingUpdate
     {
         Link = this.Link,
         NewEmail = this.NewEmail,
         NewPassword = this.NewPassword,
         NewUsername = this.NewUsername,
         User = user,
         RequestedTime = DateTime.Now
     });
 }
Beispiel #4
0
        public LMConnect.Key.Database GetDatabase(LMConnect.Key.User owner)
        {
            if (this.db_id == null)
            {
                return(null);
            }

            return(new LMConnect.Key.Database
            {
                Name = this.db_id,
                Password = this.db_password,
                Owner = owner
            });
        }
Beispiel #5
0
        public RegistrationResponse Post(RegistrationRequest request)
        {
            var id    = ShortGuid.NewGuid();
            var user  = this.GetLMConnectUser();
            var miner = new LISpMiner.LISpMiner(Config.Environment, id.ToString(), request.DbConnection, request.Metabase, request.SharedBinaries);

            Config.Environment.Register(miner);

            // is user authenticated
            if (user == null)
            {
                var owner = request.Owner;

                if (owner != null)
                {
                    // user to be registered
                    user = new LMConnect.Key.User
                    {
                        Username = owner.Username,
                        Password = owner.Password
                    };

                    this.Repository.Add(user);
                }
                else
                {
                    // use anon user
                    user = this.GetAnonymousUser();
                }
            }

            user.Miners.Add(new LMConnect.Key.Miner
            {
                Owner   = user,
                MinerId = miner.Id,
                Path    = miner.LMPrivatePath
            });

            this.Repository.Save(user);

            return(new RegistrationResponse {
                Id = id
            });
        }
        public RegistrationResponse Post(RegistrationRequest request)
        {
            var id = ShortGuid.NewGuid();
            var user = this.GetLMConnectUser();
            var miner = new LISpMiner.LISpMiner(Config.Environment, id.ToString(), request.DbConnection, request.Metabase, request.SharedBinaries);

            Config.Environment.Register(miner);

            // is user authenticated
            if (user == null)
            {
                var owner = request.Owner;

                if (owner != null)
                {
                    // user to be registered
                    user = new LMConnect.Key.User
                    {
                        Username = owner.Username,
                        Password = owner.Password
                    };

                    this.Repository.Add(user);
                }
                else
                {
                    // use anon user
                    user = this.GetAnonymousUser();
                }
            }

            user.Miners.Add(new LMConnect.Key.Miner
            {
                Owner = user,
                MinerId = miner.Id,
                Path = miner.LMPrivatePath
            });

            this.Repository.Save(user);

            return new RegistrationResponse { Id = id };
        }
        public ActionResult Index(LogInViewModel model, string returnUrl)
        {
            if (this.ModelState.IsValid)
            {
                LMConnect.Key.User user = this.Repository.Query <LMConnect.Key.User>()
                                          .FirstOrDefault(u => u.Username == model.UserName && u.Password == model.Password);

                if (user != null && user.IsAdmin)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false);

                    return(this.Redirect(returnUrl));
                }
            }

            this.ViewBag.ReturnUrl = returnUrl;
            this.ModelState.AddModelError("", "The user name or password provided is incorrect.");

            return(this.View(model));
        }
        public DatabaseResponse Post(UserRequest request, string username)
        {
            var user     = this.GetLMConnectUser();
            var database = request.GetDatabase(user);

            if (user == null)
            {
                var owner = NotRegisteredUser.FromRequest(this.Request.Content.Headers);

                if (owner != null)
                {
                    // user to be registered
                    user = new LMConnect.Key.User
                    {
                        Username = owner.Username,
                        Password = owner.Password
                    };

                    this.Repository.Add(user);
                }
                else
                {
                    return(ThrowHttpReponseException <DatabaseResponse>(
                               "No user for database.",
                               HttpStatusCode.BadRequest));
                }
            }

            if (database != null)
            {
                user.Databases.Add(database);

                this.Repository.Save(database);

                return(new DatabaseResponse(database));
            }

            return(ThrowHttpReponseException <DatabaseResponse>(
                       "No database to register",
                       HttpStatusCode.BadRequest));
        }
Beispiel #9
0
        public Response Delete(string username)
        {
            if (this.User.Identity.Name == username || this.User.IsInRole("admin"))
            {
                LMConnect.Key.User user = this.Repository.Query <LMConnect.Key.User>()
                                          .FirstOrDefault(u => u.Username == username);

                if (user != null)
                {
                    this.Repository.Remove(user);

                    return(new Response(string.Format("User \"{0}\" removed.", user.Username)));
                }

                return(this.ThrowHttpReponseException(
                           string.Format("User \"{0}\" not found.", username),
                           HttpStatusCode.NotFound));
            }

            return(this.ThrowHttpReponseException(
                       string.Format("User \"{0}\" not found or you are not auhtorized to delete him.", username),
                       HttpStatusCode.NotFound));
        }
Beispiel #10
0
        public UserResponse Put(UserRequest request)
        {
            var user = this.GetLMConnectUser();

            if (user.Username == request.name)
            {
                // updating himself
                if (!string.IsNullOrEmpty(request.new_name))
                {
                    user.Username = request.new_name;
                }

                if (!string.IsNullOrEmpty(request.new_password))
                {
                    user.Password = request.new_password;
                }

                this.Repository.Save(user);

                return(new UserResponse(user));
            }
            else if (this.User.IsInRole("admin"))
            {
                // updating by admin
                LMConnect.Key.User modified = this.Repository.Query <LMConnect.Key.User>()
                                              .FirstOrDefault(u => u.Username == request.name);

                return(this.ThrowHttpReponseException <UserResponse>(
                           "This feature is not yet implemented",
                           HttpStatusCode.NotImplemented));
            }

            return(this.ThrowHttpReponseException <UserResponse>(
                       string.Format("User \"{0}\" not found or you are not auhtorized to modify him.", request.name),
                       HttpStatusCode.NotFound));
        }
        public DatabaseResponse Post(UserRequest request, string username)
        {
            var user = this.GetLMConnectUser();
            var database = request.GetDatabase(user);

            if (user == null)
            {
                var owner = NotRegisteredUser.FromRequest(this.Request.Content.Headers);

                if (owner != null)
                {
                    // user to be registered
                    user = new LMConnect.Key.User
                    {
                        Username = owner.Username,
                        Password = owner.Password
                    };

                    this.Repository.Add(user);
                }
                else
                {
                    return ThrowHttpReponseException<DatabaseResponse>(
                        "No user for database.",
                        HttpStatusCode.BadRequest);
                }
            }

            if (database != null)
            {
                user.Databases.Add(database);

                this.Repository.Save(database);

                return new DatabaseResponse(database);
            }

            return ThrowHttpReponseException<DatabaseResponse>(
                "No database to register",
                HttpStatusCode.BadRequest);
        }
 public UserResponse(LMConnect.Key.User user)
 {
     User = user;
 }