private DeviceLoginResponse LoadTokenFromCache() { if (!File.Exists("login.json")) { return(null); } _token = JsonConvert.DeserializeObject <DeviceLoginResponse>(File.ReadAllText("login.json")); return(_token); }
public IActionResult Renew() { try { _logger.LogInformation($"POST /v1/Auth/Renew called"); var userId = _accountService.GetCurrentUserId(); var loginType = _accountService.GetCurrentUserType(); var user = _accountService.GetUserById(userId); if (loginType == "Device") { var aquariumId = _accountService.GetCurrentAquariumId(); var aquarium = _aquariumService.GetAquariumById(aquariumId); string token = _accountService.IssueDeviceLoginToken(user, aquariumId); aquarium.Device = _aquariumService.GetAquariumDeviceById(aquarium.Device.Id); var res = new DeviceLoginResponse { Account = user, Token = token, AquariumId = aquariumId, Aquarium = aquarium }; return(new OkObjectResult(res)); } else if (loginType == "User") { string token = _accountService.IssueUserLoginToken(user); var res = new { Token = token }; return(new OkObjectResult(res)); } else { return(BadRequest()); } } catch (Exception ex) { _logger.LogError($"POST /v1/Auth/Renew endpoint caught exception: { ex.Message } Details: { ex.ToString() }"); return(Unauthorized()); } }
public IActionResult DeviceLogin([FromBody] DeviceLoginRequest deviceLogin) { try { _logger.LogInformation($"POST /v1/Auth/Login/Device called"); var user = _accountService.AttemptUserCredentials(deviceLogin.Email, deviceLogin.Password); var token = _accountService.IssueDeviceLoginToken(user, deviceLogin.AquariumId); user.Aquariums = _aquariumService.GetAquariumsByAccountId(user.Id); var data = new DeviceLoginResponse { Account = user, Token = token, AquariumId = deviceLogin.AquariumId }; if (deviceLogin.AquariumId.HasValue) { var aq = user.Aquariums.First(a => a.Id == deviceLogin.AquariumId); //Register account login activity _activityService.RegisterActivity(new DeviceLoginAccountActivity() { AccountId = user.Id }); //Dispatch notification stating new login occured _notificationService.EmitAsync(new DispatchedNotification { Date = DateTime.Now.ToUniversalTime(), Type = NotificationTypes.LoginDeviceActivity, DispatcherId = user.Id, Title = "New Device Login", Subtitle = $"Aquarium monitoring device connected to {aq.Name}", }, new List <int>() { user.Id }).Wait(); //If the aquarium did not have a device, create a record for it if (aq.Device == null) { var d = new AquariumDevice { Name = "Remote Login", Type = "RaspberryPi", Address = "", Port = "0", PrivateKey = "", AquariumId = aq.Id }; aq.Device = _aquariumService.AddAquariumDevice(d); } else { aq.Device = _aquariumService.GetAquariumDeviceById(aq.Device.Id); } data.Aquarium = aq; } return(new OkObjectResult(data)); } catch (Exception ex) { _logger.LogError($"POST /v1/Auth/Login/Device endpoint caught exception: { ex.Message } Details: { ex.ToString() }"); return(Unauthorized()); } }
private void SaveTokenToCache(DeviceLoginResponse deviceLoginResponse) { _token = deviceLoginResponse; File.WriteAllText("login.json", JsonConvert.SerializeObject(deviceLoginResponse)); }
public void Logout() { _token = null; File.Delete("login.json"); _bootstrapCleanup(); }