示例#1
0
        public async Task <IActionResult> Login([FromBody] User user)
        {
            try
            {
                if (HttpContext.Connection.RemoteIpAddress != null)
                {
                    var ip = HttpContext.Connection.RemoteIpAddress.ToString();
                    if (string.IsNullOrEmpty(ip))
                    {
                        ip = Text.Unknown;
                    }
                    var responseOnLogin = await _handler.Login(user, ip);

                    return(StatusCode(StatusCodes.Status202Accepted, responseOnLogin.ToString()));
                }
            }
            catch (Exception e)
            {
                if (e is ServerException)
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, new ErrorResponse(error: Text.ServerException, errorMessage: e.ToString()).ToString()));
                }
                return(StatusCode(StatusCodes.Status500InternalServerError, new ErrorResponse(error: Text.InternalServerError, errorMessage: e.ToString()).ToString()));
            }
            return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResponse(error: Text.Forbidden).ToString()));
        }
示例#2
0
        public void CanHandleOpenRegistry()
        {
            var auth = new AuthHandler(new DictCache <Authorization>());

            auth.Login("http://localhost:5000");

            Assert.IsTrue(auth.AnonymousMode);
        }
示例#3
0
 public IHttpActionResult GetAuth(string username, string password)
 {
     (int UserID, string SessionKey)sessionInformation = AuthHandler.Login(username, password, db);
     if (sessionInformation.SessionKey == null)
     {
         return(NotFound());
     }
     return(Ok(sessionInformation));
 }
示例#4
0
        public void CanLoginWithUsername()
        {
            var auth = new AuthHandler(new DictCache <Authorization>());

            Assert.AreEqual(null, auth.Service);

            auth.Login(Settings.Registry, Settings.User, Settings.Password);

            Assert.AreEqual(Settings.Registry, auth.Service);
        }
示例#5
0
        public void CanLoginAnonymously()
        {
            var auth = new AuthHandler(new DictCache <Authorization>());

            Assert.AreEqual(null, auth.Service);

            auth.Login(Registry.DockerHub);

            Assert.AreEqual("registry.docker.io", auth.Service);
        }
示例#6
0
        public void CanGetAuthorization()
        {
            var auth = new AuthHandler(new DictCache <Authorization>());

            auth.Login(Registry.DockerHub);

            string scope = "repository:library/ubuntu:pull";

            Assert.IsNull(auth.GetAuthorization(scope));
            Assert.IsTrue(auth.UpdateAuthorization(scope));
            Assert.IsNotNull(auth.GetAuthorization(scope));
        }
示例#7
0
        public void CanHandleFailedAuthorization()
        {
            var auth = new AuthHandler(new DictCache <Authorization>());

            auth.Login(Registry.DockerHub);

            string scope = "registry:catalog:*";

            Assert.IsNull(auth.GetAuthorization(scope));
            Assert.IsFalse(auth.UpdateAuthorization(scope));
            Assert.IsNull(auth.GetAuthorization(scope));
        }
示例#8
0
        public async Task <IActionResult> Login([FromBody] User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new ErrorResponse(error: ModelState.Values.ToString())));
            }
            try
            {
                var responseOnLogin = await _handler.Login(user);

                return(StatusCode(StatusCodes.Status202Accepted, responseOnLogin.ToString()));
            }
            catch (ServerException e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, new ErrorResponse(error: "Internal server error", errorMessage: e.ToString()).ToString()));
            }
        }
示例#9
0
        public async Task <string> Login(UserCredentialDTO credential)
        {
            var hander = new AuthHandler();
            var user   = await hander.Login(credential);

            if (user == null)
            {
                Response.StatusCode = 404;
                return("");
            }

            else
            {
                Response.StatusCode = 200;
                Session["user"]     = user;
                return("ok");
            }
        }
示例#10
0
        public IActionResult Post([FromBody] RegistryCredentials credentials)
        {
            // must specify a registry
            if (string.IsNullOrEmpty(credentials.Registry))
            {
                return(Unauthorized());
            }

            // deny requests for foreign instances, if configured
            if (!string.IsNullOrEmpty(Config.Catalog?.Registry) && credentials.Registry.ToLowerInvariant() != Config.Catalog.Registry.ToLowerInvariant())
            {
                return(Unauthorized());
            }
            try
            {
                var handler = new AuthHandler(_Cache);
                handler.Login(credentials.Registry, credentials.Username, credentials.Password);
                var json       = JsonConvert.SerializeObject(credentials);
                var cipherText = _Crypto.Encrypt(json);

                return(Ok(new
                {
                    token = Jose.JWT.Encode(new Token
                    {
                        Crd = cipherText,
                        Usr = credentials.Username,
                        Reg = credentials.Registry,
                        Iat = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
                        Exp = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + Config.Security.TokenLifetime
                    }, _Crypto.ToDotNetRSA(), Jose.JwsAlgorithm.RS256)
                }));
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Error authenticating token request.");
                return(Unauthorized());
            }
        }