Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload token handling fixes. #11

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions Drivers/AlususNet.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
def servicePort: Int;
def settingsFilePath: String;
def products: Json;
def authKey: String;

handler this~init(projectName: String, servicePort: Int) {
this.projectName = projectName;
Expand All @@ -33,15 +34,15 @@
handler (this:Driver).publish() set_ptr {
this.settingsFilePath = myWorkDir + String("/.deploy.settings");
this.fetchProducts();
def authToken: String = this.getUploadToken();
this.setupProject(authToken);
this.uploadProject(authToken);
this.fetchUploadToken();
this.setupProject();
this.uploadProject();
}

macro handle401Error {
Console.print(I18n.string("\nExisting auth key is no longer valid. Requesting a new auth key.\n"));
Fs.remove(this.settingsFilePath);
authKey = this.getUploadToken();
this.fetchUploadToken();
}

handler this.fetchProducts() {
Expand Down Expand Up @@ -70,10 +71,9 @@
return Json();
}

handler this.getUploadToken(): String {
def authKey: String;
handler this.fetchUploadToken() {
if Fs.exists(this.settingsFilePath) {
return Fs.readFile(this.settingsFilePath);
this.authKey = Fs.readFile(this.settingsFilePath);
}
def userName: array[Char, 100];
def password: array[Char, 100];
Expand All @@ -90,12 +90,12 @@
request.authType = "Basic";
request.authKey = Crypto.encodeBase64(String(userName~ptr) + ":" + password~ptr);
request.post(String.format(
"{\"name\": \"%s\", \"validity\": \"30\"}",
"{\"name\": \"%s\", \"validity\": 30}",
Json.escape(this.projectName).buf
));
if request.responseHttpStatus == 201 {
authKey = Json(request.responseBody)("token");
Fs.createFile(this.settingsFilePath, authKey, authKey.getLength());
this.authKey = Json(request.responseBody)("token");
Fs.createFile(this.settingsFilePath, this.authKey, this.authKey.getLength());
break;
} else if request.responseHttpStatus == 401 {
Console.print(I18n.string("Authentication error. Invalid username or password.\n"));
Expand All @@ -104,10 +104,9 @@
handleUnexpectedCommunicationError[];
}
}
return authKey;
}

handler this.setupProject(authKey: String) {
handler this.setupProject() {
def status: String;
def productId: String;
def promotionCode: String;
Expand All @@ -117,7 +116,7 @@
));
request.verbose = verbose;
request.authType = "Bearer";
request.authKey = authKey;
request.authKey = this.authKey;
request.get();
if request.responseHttpStatus == 200 {
request.responseBody.split("&").{
Expand Down Expand Up @@ -234,7 +233,7 @@
));
request.verbose = verbose;
request.authType = "Bearer";
request.authKey = authKey;
request.authKey = this.authKey;
request.post("");
if request.responseHttpStatus == 200 {
break;
Expand Down Expand Up @@ -274,7 +273,7 @@
}
}

handler this.uploadProject(authKey: String) {
handler this.uploadProject() {
def order: String = String("rm -rf \"") + this.projectName + "\"";
System.exec(order);
order = String("mv Build \"") + this.projectName + "\"";
Expand Down Expand Up @@ -305,7 +304,7 @@
Net.uriEncode(this.projectName).buf, this.servicePort
));
request.verbose = verbose;
request.authKey = authKey;
request.authKey = this.authKey;
request.authType = "Bearer";
request.contentType = "application/octet-stream";
request.addHeader(String.format("Content-Total-Length: %i", totalSize~cast[Int]));
Expand Down