Commit 95329eba authored by Vincent's avatar Vincent 💩

Got the new auth working, but dive tank pressure must be int instead of double

parent 2031edc2
......@@ -223,7 +223,7 @@ namespace divecomputer_test {
if (FileRadio.Checked) {
writer = new FileDiveWriter(SaveFileText.Text);
} else if(LogRadio.Checked) {
writer = new LittleLogDiveWriter(Session.WebAppSession.Token, CheckboxReadSinceLast.Checked);
writer = new LittleLogDiveWriter(Session.WebAppSession, CheckboxReadSinceLast.Checked);
} else {
throw new Exception("No target type selected");
}
......
......@@ -21,7 +21,7 @@ namespace LibDiveComputer {
[JsonIgnore]
public WebApplicationSession WebAppSession { get; private set; }
public string AppToken { get { return WebAppSession.Token; } }
public string AppToken { get { return WebAppSession.RefreshToken; } }
protected Configuration config;
protected bool isAcceptingChanges = false;
......
......@@ -100,6 +100,11 @@ namespace DiveLogUploader {
private string _accessToken;
private UserData _data;
public string RefreshToken {
get { return _refreshToken; }
}
public UserData Data {
get { return _data; }
set {
......@@ -133,7 +138,7 @@ namespace DiveLogUploader {
}
public async Task GetRefreshToken(string email, string password) {
public async Task Login(string email, string password) {
var d = JToken.FromObject(new {
email = email,
password = password,
......@@ -160,25 +165,55 @@ namespace DiveLogUploader {
return;
}
}
public async Task<TOUT> DoRequest<TOUT>(
public async Task<TOUT> DoRequestAsync<TOUT>(
string path,
HttpVerb verb
) {
var resp = await Request.JsonAsync<TOUT>(
BASE_URL + path,
verb,
TokenHeader(_accessToken)
);
if (_accessToken == null) {
await GetAccessTokenAsync();
}
try {
return await Request.JsonAsync<TOUT>(
BASE_URL + path,
verb,
TokenHeader(_accessToken)
);
} catch (WebserviceError ex) when (ex.Code == HttpStatusCode.Unauthorized) {
_accessToken = null;
return await DoRequestAsync<TOUT>(path, verb);
}
}
public TOUT DoRequest<TOUT>(
string path,
HttpVerb verb
) {
if (_accessToken == null) {
GetAccessToken();
}
try {
return Request.Json<TOUT>(
BASE_URL + path,
verb,
TokenHeader(_accessToken)
);
} catch (WebserviceError ex) when (ex.Code == HttpStatusCode.Unauthorized) {
_accessToken = null;
return DoRequest<TOUT>(path, verb);
}
return resp;
}
public async Task<TOUT> DoRequest<TIN, TOUT>(
public async Task<TOUT> DoRequestAsync<TIN, TOUT>(
string path,
HttpVerb verb,
TIN data = default(TIN)
) {
if (_accessToken == null) {
await GetAccessTokenAsync();
}
try {
var resp = await Request.JsonAsync<TIN, TOUT>(
......@@ -190,12 +225,46 @@ namespace DiveLogUploader {
return resp;
} catch(WebserviceError ex) when (ex.Code == HttpStatusCode.Unauthorized) {
await GetAccessToken();
await DoRequest<TIN, TOUT>(path, verb, data);
_accessToken = null;
return await DoRequestAsync<TIN, TOUT>(path, verb, data);
}
}
public TOUT DoRequest<TIN, TOUT>(
string path,
HttpVerb verb,
TIN data = default(TIN)
) {
if (_accessToken == null) {
GetAccessToken();
}
try {
var resp = Request.Json<TIN, TOUT>(
BASE_URL + path,
verb,
data,
TokenHeader(_accessToken)
);
return resp;
} catch (WebserviceError ex) when (ex.Code == HttpStatusCode.Unauthorized) {
_accessToken = null;
return DoRequest<TIN, TOUT>(path, verb, data);
}
}
private void GetAccessToken() {
var dat = Request.Json<TokenResponseData>(
BASE_URL + "auth/access-token",
HttpVerb.GET,
TokenHeader(_refreshToken)
);
_accessToken = dat.jwt;
}
private async Task GetAccessToken() {
private async Task GetAccessTokenAsync() {
var dat = await Request.JsonAsync<TokenResponseData>(
BASE_URL + "auth/access-token",
HttpVerb.GET,
......@@ -207,7 +276,7 @@ namespace DiveLogUploader {
private async Task GetData() {
try {
var result = await DoRequest<UserData>(
var result = await DoRequestAsync<UserData>(
"user/profile",
HttpVerb.GET
);
......
......@@ -36,23 +36,22 @@ namespace DiveLogUploader.Writers {
/// </summary>
/// <remarks>LittleDiveLog is a side project still under development</remarks>
public class LittleLogDiveWriter : AsyncDiveWriter {
protected string Token;
protected WebApplicationSession WebSession;
protected int ComputerId;
protected bool UseFingerprint;
public LittleLogDiveWriter(string token, bool useFingerprint): base() {
Token = token;
public LittleLogDiveWriter(WebApplicationSession sess, bool useFingerprint): base() {
WebSession = sess;
UseFingerprint = useFingerprint;
}
public override void SetDevice(Device d) {
base.SetDevice(d);
var resp = Request.Json<Computer, PostComputerResponse>(
WebApplicationSession.BASE_URL + "computer",
var resp = WebSession.DoRequest<Computer, PostComputerResponse>(
"computer",
HttpVerb.POST,
new Computer(d),
WebApplicationSession.TokenHeader(Token)
new Computer(d)
);
ComputerId = resp.computer_id;
......@@ -65,11 +64,10 @@ namespace DiveLogUploader.Writers {
protected override void ProcessDive(Dive d) {
var boundDive = new ComputerBoundDive(d, ComputerId);
var resp = Request.Json<Dive, PostDiveResponse>(
WebApplicationSession.BASE_URL + "dive",
var resp = WebSession.DoRequest<Dive, PostDiveResponse>(
"dive",
HttpVerb.POST,
boundDive,
WebApplicationSession.TokenHeader(Token)
boundDive
);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment