public NDIDCallbackRequestModel GetUserRequest(string namespaces, string identifier, string requestId)
 {
     using (LiteDatabase db = new LiteDatabase(_persistancePath))
     {
         LiteCollection <NDIDUserRequestDBModel> collection = db.GetCollection <NDIDUserRequestDBModel>(COLLECTION_REQUEST);
         NDIDUserRequestDBModel   request = collection.FindOne(x => x.Namespace == namespaces && x.Identifier == identifier && x.RequestId == requestId);
         NDIDCallbackRequestModel result  = _mapper.Map <NDIDCallbackRequestModel>(request);
         return(result);
     }
 }
Ejemplo n.º 2
0
        public void HandleIncomingRequestCallback(NDIDCallbackRequestModel model)
        {
            // check that user exist
            NDIDUserModel user = _db.FindUser(model.Namespace, model.Identifier);

            if (user == null)
            {
                throw new ApplicationException();
            }
            _db.SaveUserRequest(model.Namespace, model.Identifier, model.RequestId, model);
        }
 public IActionResult IDPRequest([FromBody] NDIDCallbackRequestModel request)
 {
     if (request.Type == NDIDConstant.CallbackType.INCOMING_REQUEST)
     {
         _ndid.HandleIncomingRequestCallback(request);
     }
     else
     {
         throw new NotImplementedException();
     }
     return(NoContent());
 }
        public void SaveAndGetUserRequest()
        {
            string namespaces = "cid";
            string identifier = "1234";
            NDIDCallbackRequestModel request = new NDIDCallbackRequestModel();

            request.RequestId      = "8707fa402ae174737a5a6cefa7e8d47b836f40fdae7f2b53297ceecda27f3b7c";
            request.RequestMsg     = "dummy Request Message";
            request.RequestMsgHash = "wl4+u6caNoCDb5nr2JPuYGmeIGZjRECCQAicomlJ38E=";
            request.Namespace      = namespaces;
            request.Identifier     = identifier;
            _db.SaveUserRequest(namespaces, identifier, request.RequestId, request);
            NDIDCallbackRequestModel actual_request = _db.GetUserRequest(namespaces, identifier, request.RequestId);

            actual_request.Should().BeEquivalentTo <NDIDCallbackRequestModel>(request);
        }
Ejemplo n.º 5
0
        public async Task CreateNewIdentity(NewIdentityModel iden)
        {
            // 1. generate new keypair
            NewIdentityModel newIdentity = new NewIdentityModel();

            newIdentity.NameSpace  = iden.NameSpace;
            newIdentity.Identifier = iden.Identifier;
            string sid = newIdentity.NameSpace + "-" + newIdentity.Identifier;
            await _dpki.GenNewKey(sid);

            // 2. read public key
            string pubKey = await _dpki.GetPubKey(sid);

            // 3. construct new identity api request
            newIdentity.AccessorType   = "RSA";
            newIdentity.AccessorPubKey = pubKey;
            newIdentity.ReferenceId    = Guid.NewGuid().ToString();
            newIdentity.CallbackUrl    = new Uri(new Uri(_config.GetCallbackPath()), "api/callback/identity").ToString();
            newIdentity.IAL            = 2.3m;
            _db.SaveAccessorSign(newIdentity.ReferenceId, sid);
            _db.SaveReference(newIdentity.ReferenceId, "sid", sid);
            // 4. check response from api reqeust
            using (HttpClient client = new HttpClient())
            {
                Uri url = new Uri(_apiServerAddress + "/v2/identity");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                string        jsonContent = JsonConvert.SerializeObject(newIdentity);
                StringContent content     = new StringContent(jsonContent, Encoding.UTF8, "application/json");
                var           result      = client.PostAsync(url, content).Result;
                string        resultJson  = await result.Content.ReadAsStringAsync();

                if (result.IsSuccessStatusCode)
                {
                    NDIDCallbackIdentityModel model = JsonConvert.DeserializeObject <NDIDCallbackIdentityModel>(resultJson);
                    _db.SaveReference(newIdentity.ReferenceId, "accessor_id", model.AccessorId);
                    _db.SaveReference(newIdentity.ReferenceId, "request_id", model.RequestId);
                }
                else
                {
                    NDIDCallbackRequestModel model = JsonConvert.DeserializeObject <NDIDCallbackRequestModel>(resultJson);
                    throw new ApplicationException(model.Error.Message);
                }
            }
        }
Ejemplo n.º 6
0
        public async Task CreateIDPResponse(string namespaces, string identifier, string requestId, string status)
        {
            // get user from parameter
            NDIDUserModel user = _db.FindUser(namespaces, identifier);

            if (user == null)
            {
                throw new ApplicationException();
            }
            // get request
            NDIDCallbackRequestModel request = _db.GetUserRequest(namespaces, identifier, requestId);

            if (request == null)
            {
                throw new ApplicationException();
            }
            // get key and sign message
            // always use first accessor keu for simplicity
            string keyName   = namespaces + "-" + identifier + "-" + "0";
            string signature = await _dpki.Sign(keyName, request.RequestMsgHash);

            // construct idp response model
            NDIDIDPResponseModel model = new NDIDIDPResponseModel();

            model.ReferenceId = Guid.NewGuid().ToString();
            model.RequestId   = request.RequestId;
            model.CallbackUrl = new Uri(new Uri(_config.GetCallbackPath()), "api/callback/response").ToString();
            model.NameSpace   = user.NameSpace;
            model.Identifier  = user.Identifier;
            model.AccessorId  = user.Accessors[0].AccessorId;
            model.Secret      = user.Accessors[0].Secret;
            model.Signature   = signature;
            model.Status      = status;
            model.IAL         = 2.3m;
            model.AAL         = 3.0m;
            // call ndid api
            using (HttpClient client = new HttpClient())
            {
                Uri url = new Uri(_apiServerAddress + "/v2/idp/response");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                string        jsonContent = JsonConvert.SerializeObject(model);
                StringContent content     = new StringContent(jsonContent, Encoding.UTF8, "application/json");
                var           result      = client.PostAsync(url, content).Result;
                string        resultJson  = await result.Content.ReadAsStringAsync();

                if (!result.IsSuccessStatusCode)
                {
                    NDIDCallbackRequestModel err = JsonConvert.DeserializeObject <NDIDCallbackRequestModel>(resultJson);
                    if (err.Error.Code == "20025" || err.Error.Code == "20026")
                    {
                        _db.RemoveUserRequest(model.RequestId);
                        throw new ApplicationException("remove");
                    }
                    else
                    {
                        throw new ApplicationException(err.Error.Message);
                    }
                }
            }
        }
Ejemplo n.º 7
0
 public Task HandleIncomingRequestCallbackAsync(NDIDCallbackRequestModel model)
 {
     throw new NotImplementedException();
 }
 public long SaveUserRequest(string namespaces, string identifier, string requestId, NDIDCallbackRequestModel request)
 {
     using (LiteDatabase db = new LiteDatabase(_persistancePath))
     {
         LiteCollection <NDIDUserRequestDBModel> collection = db.GetCollection <NDIDUserRequestDBModel>(COLLECTION_REQUEST);
         NDIDUserRequestDBModel model = _mapper.Map <NDIDUserRequestDBModel>(request);
         long id = collection.Insert(model);
         return(id);
     }
 }