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())); }
public void CanHandleOpenRegistry() { var auth = new AuthHandler(new DictCache <Authorization>()); auth.Login("http://localhost:5000"); Assert.IsTrue(auth.AnonymousMode); }
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)); }
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); }
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); }
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)); }
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)); }
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())); } }
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"); } }
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()); } }