Пример #1
0
        public async ValueTask <HttpResponseMessage> DeviceCode_AskV2(DeviceCodeAskV2 model)
        {
            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair <string, string>("issuer", model.issuer),
                new KeyValuePair <string, string>("client", model.client),
                new KeyValuePair <string, string>("grant_type", model.grant_type),
                new KeyValuePair <string, string>("user", model.user),
            });

            return(await _http.PostAsync("oauth2/v2/dcg-ask", content));
        }
Пример #2
0
        public async ValueTask <DeviceCodeV2> DeviceCode_AskV2(DeviceCodeAskV2 model)
        {
            var response = await Endpoints.DeviceCode_AskV2(model);

            if (response.IsSuccessStatusCode)
            {
                return(await response.Content.ReadAsAsync <DeviceCodeV2>().ConfigureAwait(false));
            }

            throw new HttpRequestException(response.RequestMessage.ToString(),
                                           new Exception(response.ToString()));
        }
Пример #3
0
        public IActionResult DeviceCodeV2_Ask([FromForm] DeviceCodeAskV2 input)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Guid       issuerID;
            tbl_Issuer issuer;

            //check if identifier is guid. resolve to guid if not.
            if (Guid.TryParse(input.issuer, out issuerID))
            {
                issuer = uow.Issuers.Get(x => x.Id == issuerID).SingleOrDefault();
            }
            else
            {
                issuer = uow.Issuers.Get(x => x.Name == input.issuer).SingleOrDefault();
            }

            if (issuer == null)
            {
                ModelState.AddModelError(MessageType.IssuerNotFound.ToString(), $"Issuer:{input.issuer}");
                return(NotFound(ModelState));
            }

            Guid         audienceID;
            tbl_Audience audience;

            //check if identifier is guid. resolve to guid if not.
            if (Guid.TryParse(input.client, out audienceID))
            {
                audience = uow.Audiences.Get(x => x.Id == audienceID).SingleOrDefault();
            }
            else
            {
                audience = uow.Audiences.Get(x => x.Name == input.client).SingleOrDefault();
            }

            if (audience == null)
            {
                ModelState.AddModelError(MessageType.AudienceNotFound.ToString(), $"Audience:{input.client}");
                return(NotFound(ModelState));
            }

            Guid     userID;
            tbl_User user;

            //check if identifier is guid. resolve to guid if not.
            if (Guid.TryParse(input.user, out userID))
            {
                user = uow.Users.Get(x => x.Id == userID).SingleOrDefault();
            }
            else
            {
                user = uow.Users.Get(x => x.UserName == input.user).SingleOrDefault();
            }

            if (user == null)
            {
                ModelState.AddModelError(MessageType.UserNotFound.ToString(), $"User:{input.user}");
                return(NotFound(ModelState));
            }

            var expire = uow.Settings.Get(x => x.IssuerId == issuer.Id && x.AudienceId == null && x.UserId == null &&
                                          x.ConfigKey == SettingsConstants.TotpExpire).Single();

            var polling = uow.Settings.Get(x => x.IssuerId == issuer.Id && x.AudienceId == null && x.UserId == null &&
                                           x.ConfigKey == SettingsConstants.PollingMax).Single();

            var authorize = new Uri(string.Format("{0}/{1}/{2}", conf["IdentityMeUrls:BaseUiUrl"], conf["IdentityMeUrls:BaseUiPath"], "authorize"));
            var nonce     = Base64.CreateString(32);

            //create domain model for this result type...
            var result = new DeviceCodeV2()
            {
                issuer           = issuer.Id.ToString(),
                client           = audience.Id.ToString(),
                verification_url = authorize.AbsoluteUri,
                user_code        = new TimeBasedTokenFactory(8, 10).Generate(user.SecurityStamp, user.Id.ToString()),
                device_code      = nonce,
                interval         = uint.Parse(polling.ConfigValue),
            };

            var state = uow.States.Create(
                map.Map <tbl_State>(new StateV1()
            {
                IssuerId     = issuer.Id,
                AudienceId   = audience.Id,
                UserId       = user.Id,
                StateValue   = nonce,
                StateType    = ConsumerType.Device.ToString(),
                StateConsume = false,
                ValidFromUtc = DateTime.UtcNow,
                ValidToUtc   = DateTime.UtcNow.AddSeconds(uint.Parse(expire.ConfigValue)),
            }));

            uow.Commit();

            return(Ok(result));
        }