public async Task HandleCreateIdentityResultCallbackAsync(NDIDCallbackIdentityModel model) { if (model.IsSuccess) { string sid = _db.GetReferecne(model.ReferenceId, "sid"); string[] parts = sid.Split('-'); NDIDUserModel user = new NDIDUserModel(); user.NameSpace = parts[0]; user.Identifier = parts[1]; string accessor_id = _db.GetReferecne(model.ReferenceId, "accessor_id"); NDIDAccessorModel accessor = new NDIDAccessorModel(); accessor.AccessorId = accessor_id; accessor.Secret = model.Secret; // update key string newKeyName = sid + "-" + "0"; // not use base64 file name because windows cannot support filename with "/" charactor _dpki.UpdateKey(sid, newKeyName); string pubKey = await _dpki.GetPubKey(newKeyName); accessor.AccessorPubKey = pubKey; user.Accessors.Add(accessor); // save new user _db.CreateNewUser(user); // remove all referenceId _db.RemoveReference(model.ReferenceId); } else { throw new ApplicationException(); } }
public NDIDUserModel FindUser(string namespaces, string identifier) { using (LiteDatabase db = new LiteDatabase(_persistancePath)) { LiteCollection <NDIDUserModel> collection = db.GetCollection <NDIDUserModel>(COLLECTION_USER); NDIDUserModel model = collection.FindOne(x => x.NameSpace == namespaces && x.Identifier == identifier); return(model); } }
public long CreateNewUser(NDIDUserModel user) { using (LiteDatabase db = new LiteDatabase(_persistancePath)) { LiteCollection <NDIDUserDBModel> collection = db.GetCollection <NDIDUserDBModel>(COLLECTION_USER); NDIDUserDBModel model = _mapper.Map <NDIDUserDBModel>(user); long id = collection.Insert(model); return(id); } }
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 void SaveAndFindNDIDUser() { NDIDUserModel user = new NDIDUserModel(); user.NameSpace = "cid"; user.Identifier = "1234"; user.Accessors.Add(new NDIDAccessorModel { AccessorId = "hello", Secret = "this should be secret" }); long id = _db.CreateNewUser(user); NDIDUserModel retrievedUser = _db.FindUser(user.NameSpace, user.Identifier); retrievedUser.Should().BeEquivalentTo <NDIDUserModel>(user); }
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); } } } }