public IActionResult ClientCredentialV1_Refresh([FromForm] RefreshTokenV1 input) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } return(StatusCode((int)HttpStatusCode.NotImplemented)); }
public async ValueTask <UserJwtV1> ResourceOwner_RefreshV1(RefreshTokenV1 model) { var response = await Endpoints.ResourceOwner_RefreshV1(model); if (response.IsSuccessStatusCode) { return(await response.Content.ReadAsAsync <UserJwtV1>().ConfigureAwait(false)); } throw new HttpRequestException(response.RequestMessage.ToString(), new Exception(response.ToString())); }
public async ValueTask <HttpResponseMessage> ResourceOwner_RefreshV1(RefreshTokenV1 model) { var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("issuer_id", model.issuer_id), new KeyValuePair <string, string>("client_id", model.client_id), new KeyValuePair <string, string>("grant_type", model.grant_type), new KeyValuePair <string, string>("refresh_token", model.refresh_token), }); return(await _http.PostAsync("oauth2/v1/ropg-rt", content)); }
public IActionResult ResourceOwnerV1_Refresh([FromForm] RefreshTokenV1 input) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var refresh = uow.Refreshes.Get(QueryExpressionFactory.GetQueryExpression <tbl_Refresh>() .Where(x => x.RefreshValue == input.refresh_token).ToLambda()).SingleOrDefault(); if (refresh == null) { ModelState.AddModelError(MessageType.TokenInvalid.ToString(), $"Token:{input.refresh_token}"); return(NotFound(ModelState)); } else if (!string.Equals(refresh.RefreshType, ConsumerType.User.ToString(), StringComparison.OrdinalIgnoreCase) || (refresh.ValidFromUtc >= DateTime.UtcNow || refresh.ValidToUtc <= DateTime.UtcNow)) { ModelState.AddModelError(MessageType.TokenInvalid.ToString(), $"Token:{input.refresh_token}"); return(BadRequest(ModelState)); } Guid issuerID; tbl_Issuer issuer; //check if identifier is guid. resolve to guid if not. if (Guid.TryParse(input.issuer_id, out issuerID)) { issuer = uow.Issuers.Get(x => x.Id == issuerID).SingleOrDefault(); } else { issuer = uow.Issuers.Get(x => x.Name == input.issuer_id).SingleOrDefault(); } if (issuer == null) { ModelState.AddModelError(MessageType.IssuerNotFound.ToString(), $"Issuer:{input.issuer_id}"); return(NotFound(ModelState)); } else if (!issuer.IsEnabled) { ModelState.AddModelError(MessageType.IssuerInvalid.ToString(), $"Issuer:{issuer.Id}"); return(BadRequest(ModelState)); } Guid audienceID; tbl_Audience audience; //check if identifier is guid. resolve to guid if not. if (Guid.TryParse(input.client_id, out audienceID)) { audience = uow.Audiences.Get(x => x.Id == audienceID).SingleOrDefault(); } else { audience = uow.Audiences.Get(x => x.Name == input.client_id).SingleOrDefault(); } if (audience == null) { ModelState.AddModelError(MessageType.AudienceNotFound.ToString(), $"Audience:{input.client_id}"); return(NotFound(ModelState)); } else if (audience.IsLockedOut) { ModelState.AddModelError(MessageType.AudienceInvalid.ToString(), $"Audience:{audience.Id}"); return(BadRequest(ModelState)); } var user = uow.Users.Get(x => x.Id == refresh.UserId).SingleOrDefault(); //check that user exists... if (user == null) { ModelState.AddModelError(MessageType.UserNotFound.ToString(), $"User:{refresh.UserId}"); return(NotFound(ModelState)); } //check that user is not locked... else if (uow.Users.IsLockedOut(user) || !user.EmailConfirmed || !user.PasswordConfirmed) { ModelState.AddModelError(MessageType.UserInvalid.ToString(), $"User:{user.Id}"); return(BadRequest(ModelState)); } var rop_claims = uow.Users.GenerateAccessClaims(issuer, user); var rop = auth.ResourceOwnerPassword(issuer.Name, issuer.IssuerKey, conf["IdentityTenant:Salt"], new List <string>() { audience.Name }, rop_claims); var rt_claims = uow.Users.GenerateRefreshClaims(issuer, user); var rt = auth.ResourceOwnerPassword(issuer.Name, issuer.IssuerKey, conf["IdentityTenant:Salt"], new List <string>() { audience.Name }, rt_claims); uow.Refreshes.Create( map.Map <tbl_Refresh>(new RefreshV1() { IssuerId = issuer.Id, UserId = user.Id, RefreshType = ConsumerType.User.ToString(), RefreshValue = rt.RawData, IssuedUtc = rt.ValidFrom, ValidFromUtc = rt.ValidFrom, ValidToUtc = rt.ValidTo, })); uow.AuthActivity.Create( map.Map <tbl_AuthActivity>(new AuthActivityV1() { UserId = user.Id, LoginType = GrantFlowType.RefreshTokenV1.ToString(), LoginOutcome = GrantFlowResultType.Success.ToString(), })); uow.Commit(); var result = new UserJwtV1() { token_type = "bearer", access_token = rop.RawData, refresh_token = rt.RawData, user_id = user.UserName, client_id = audience.Name, issuer_id = issuer.Name + ":" + conf["IdentityTenant:Salt"], expires_in = (int)(new DateTimeOffset(rop.ValidTo).Subtract(DateTime.UtcNow)).TotalSeconds, }; return(Ok(result)); }