108 lines
3.6 KiB
Ziran
108 lines
3.6 KiB
Ziran
// Account deletion uses a fresh challenge and account signature. No private
|
|
// key or exported backup is sent to Mesh.
|
|
#import "mesh_client/client"
|
|
#import "mesh_client/auth"
|
|
#import "mesh_client/url"
|
|
#import "mesh_client/wire"
|
|
#import "net_http"
|
|
#import "text_buffer"
|
|
|
|
#scope_export
|
|
#program_export
|
|
BuildDeleteBody :: (account_id: string, public_key: string,
|
|
output: []u8) -> bool {
|
|
if output.count == 0 { return false }
|
|
output[0] = cast(u8)0
|
|
if !Hex64(account_id) || public_key.count == 0 { return false }
|
|
writer: Writer = Writer.{output, 0, true}
|
|
writer = WriteText(writer, "{\"user_id_hash\":")
|
|
writer = AppendJsonQuoted(writer, account_id)
|
|
writer = WriteText(writer, ",\"public_key\":")
|
|
writer = AppendJsonQuoted(writer, public_key)
|
|
writer = WriteByte(writer, cast(u8)125)
|
|
if !writer.valid {
|
|
output[0] = cast(u8)0
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Shared signed deletion preparation for blocking and polled hosts.
|
|
PrepareDelete :: (client: Client, challenge: string,
|
|
body: []u8, signature: []u8) -> AuthResult {
|
|
nonce: [65]u8
|
|
if !FieldText(challenge, "nonce", nonce[:]) ||
|
|
!Hex64(TextUntilNul(nonce[:])) {
|
|
return cast(AuthResult)AuthResult.AUTH_CHALLENGE_FAILED
|
|
}
|
|
if !BuildDeleteBody(client.identity.account_id,
|
|
client.identity.public_key, body) {
|
|
return cast(AuthResult)AuthResult.AUTH_PAYLOAD_FAILED
|
|
}
|
|
body_hash: [65]u8
|
|
if !client.digest(TextUntilNul(body), body_hash[:]) ||
|
|
!Hex64(TextUntilNul(body_hash[:])) {
|
|
return cast(AuthResult)AuthResult.AUTH_SIGN_FAILED
|
|
}
|
|
message: [256]u8
|
|
if !BuildDeleteMessageHash(TextUntilNul(body_hash[:]),
|
|
TextUntilNul(nonce[:]), message[:]) {
|
|
return cast(AuthResult)AuthResult.AUTH_SIGN_FAILED
|
|
}
|
|
if !client.signer(client.identity.signing_context,
|
|
TextUntilNul(message[:]), signature) ||
|
|
!HexText(TextUntilNul(signature), 4840) {
|
|
return cast(AuthResult)AuthResult.AUTH_SIGN_FAILED
|
|
}
|
|
return cast(AuthResult)AuthResult.AUTH_OK
|
|
}
|
|
|
|
#scope_export
|
|
#program_export
|
|
DeleteAccount :: (client: Client) -> AuthResult {
|
|
challenge: [4096]u8
|
|
challenged := FetchChallengeJson(client, challenge[:])
|
|
if challenged != cast(AuthResult)AuthResult.AUTH_OK {
|
|
return challenged
|
|
}
|
|
body: [4096]u8
|
|
signature: [4841]u8
|
|
prepared := PrepareDelete(client, TextUntilNul(challenge[:]),
|
|
body[:], signature[:])
|
|
if prepared != cast(AuthResult)AuthResult.AUTH_OK {
|
|
return prepared
|
|
}
|
|
server: [512]u8
|
|
url: [768]u8
|
|
if !NormalizeUrl(client.server_url, server[:]) ||
|
|
!JoinUrl(TextUntilNul(server[:]),
|
|
"/api/v1/account/delete", url[:]) {
|
|
return cast(AuthResult)AuthResult.AUTH_INVALID_URL
|
|
}
|
|
headers: [2]HttpHeader
|
|
headers[0].name = "X-Daochi-User"
|
|
headers[0].value = client.identity.account_id
|
|
headers[1].name = "X-Daochi-Signature"
|
|
headers[1].value = TextUntilNul(signature[:])
|
|
request: HttpRequest
|
|
request.method = "POST"
|
|
request.url = TextUntilNul(url[:])
|
|
request.content_type = "application/json"
|
|
request.accept = "application/json"
|
|
request.body = TextUntilNul(body[:])
|
|
request.headers = headers[:]
|
|
response: [4096]u8
|
|
status: s32 = 0
|
|
if !client.send(client.transport_context, request,
|
|
response[:], *status) {
|
|
return cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED
|
|
}
|
|
if status == 401 || status == 403 {
|
|
return cast(AuthResult)AuthResult.AUTH_FAILED
|
|
}
|
|
if status < 200 || status >= 300 {
|
|
return cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED
|
|
}
|
|
return cast(AuthResult)AuthResult.AUTH_OK
|
|
}
|