270 lines
8.4 KiB
Ziran
270 lines
8.4 KiB
Ziran
// Synchronous Mesh client entry points. Hosts provide the bounded request
|
|
// callback, so this module contains no platform or application implementation.
|
|
#import "mesh_client/auth"
|
|
#import "mesh_client/url"
|
|
#import "mesh_client/wire"
|
|
#import "net_http"
|
|
#import "text_buffer"
|
|
|
|
SendRequest :: #type (context: *void, request: HttpRequest,
|
|
output: []u8, status: *s32) -> bool #c_call;
|
|
|
|
Client :: struct {
|
|
server_url: string
|
|
app_id: string
|
|
identity: Identity
|
|
digest: DigestBody
|
|
signer: SignMessage
|
|
send: SendRequest
|
|
transport_context: *void
|
|
}
|
|
|
|
#scope_export
|
|
#program_export
|
|
FetchChallengeJson :: (client: Client, output: []u8) -> AuthResult {
|
|
if output.count == 0 { return cast(AuthResult)AuthResult.AUTH_PAYLOAD_FAILED }
|
|
output[0] = cast(u8)0
|
|
server: [512]u8
|
|
if !NormalizeUrl(client.server_url, server[:]) {
|
|
return cast(AuthResult)AuthResult.AUTH_INVALID_URL
|
|
}
|
|
path: [128]u8
|
|
if !BuildChallengePath(client.identity.account_id, path[:]) {
|
|
return cast(AuthResult)AuthResult.AUTH_NO_ACCOUNT
|
|
}
|
|
url: [768]u8
|
|
if !JoinUrl(TextUntilNul(server[:]),
|
|
TextUntilNul(path[:]), url[:]) {
|
|
return cast(AuthResult)AuthResult.AUTH_INVALID_URL
|
|
}
|
|
|
|
request: HttpRequest
|
|
request.method = "GET"
|
|
request.url = TextUntilNul(url[:])
|
|
request.accept = "application/json"
|
|
status: s32 = 0
|
|
if !client.send(client.transport_context, request,
|
|
output, *status) || status != 200 {
|
|
if status == 401 {
|
|
return cast(AuthResult)AuthResult.AUTH_FAILED
|
|
}
|
|
return cast(AuthResult)AuthResult.AUTH_CHALLENGE_FAILED
|
|
}
|
|
return cast(AuthResult)AuthResult.AUTH_OK
|
|
}
|
|
|
|
#scope_export
|
|
#program_export
|
|
Login :: (client: Client, session: *Session) -> AuthResult {
|
|
if session == null {
|
|
return cast(AuthResult)AuthResult.AUTH_PAYLOAD_FAILED
|
|
}
|
|
session.* = Session.{}
|
|
challenge: [4096]u8
|
|
challenged := FetchChallengeJson(client, challenge[:])
|
|
if challenged != cast(AuthResult)AuthResult.AUTH_OK {
|
|
return challenged
|
|
}
|
|
|
|
body: [4096]u8
|
|
signature: [4841]u8
|
|
prepared := PrepareLogin(client.identity,
|
|
TextUntilNul(challenge[:]), client.digest, client.signer,
|
|
body[:], signature[:])
|
|
if prepared != cast(AuthResult)AuthResult.AUTH_OK {
|
|
return prepared
|
|
}
|
|
server: [512]u8
|
|
if !NormalizeUrl(client.server_url, server[:]) {
|
|
return cast(AuthResult)AuthResult.AUTH_INVALID_URL
|
|
}
|
|
url: [768]u8
|
|
if !JoinUrl(TextUntilNul(server[:]),
|
|
"/api/v1/sync/login", url[:]) {
|
|
return cast(AuthResult)AuthResult.AUTH_INVALID_URL
|
|
}
|
|
|
|
request: HttpRequest
|
|
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.method = "POST"
|
|
request.url = TextUntilNul(url[:])
|
|
request.accept = "application/json"
|
|
request.content_type = "application/json"
|
|
request.body = TextUntilNul(body[:])
|
|
request.headers = headers[:]
|
|
response: [8192]u8
|
|
status: s32 = 0
|
|
if !client.send(client.transport_context, request,
|
|
response[:], *status) {
|
|
return cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED
|
|
}
|
|
return AcceptLogin(status, TextUntilNul(response[:]), session)
|
|
}
|
|
|
|
MethodValid :: (method: string) -> bool {
|
|
if method.count == 0 || method.count >= 32 {
|
|
return false
|
|
}
|
|
index: s64 = 0
|
|
while index < cast(s64)method.count {
|
|
byte := method[index]
|
|
if !((byte >= cast(u8)65 && byte <= cast(u8)90) ||
|
|
(byte >= cast(u8)97 && byte <= cast(u8)122) ||
|
|
byte == cast(u8)45) {
|
|
return false
|
|
}
|
|
index += 1
|
|
}
|
|
return true
|
|
}
|
|
|
|
PathValid :: (path: string) -> bool {
|
|
if path.count == 0 || path[0] != cast(u8)47 {
|
|
return false
|
|
}
|
|
index: s64 = 0
|
|
while index < cast(s64)path.count {
|
|
byte := path[index]
|
|
if byte <= cast(u8)32 || byte >= cast(u8)127 ||
|
|
byte == cast(u8)35 {
|
|
return false
|
|
}
|
|
index += 1
|
|
}
|
|
return true
|
|
}
|
|
|
|
TokenValid :: (session: *Session, now: s64) -> bool {
|
|
return session.token[0] != cast(u8)0 &&
|
|
session.expires_at_local > now
|
|
}
|
|
|
|
SetExpiry :: (session: *Session, now: s64) {
|
|
session.login_local_time = now
|
|
session.expires_at_local = now
|
|
if now < 0 || session.expires_in_seconds <= 30 ||
|
|
session.expires_in_seconds > 9007199254740991 - now {
|
|
return
|
|
}
|
|
session.expires_at_local = now + session.expires_in_seconds - 30
|
|
}
|
|
|
|
#scope_export
|
|
EnsureSession :: (client: Client, session: *Session,
|
|
now: s64) -> AuthResult {
|
|
if session == null {
|
|
return cast(AuthResult)AuthResult.AUTH_PAYLOAD_FAILED
|
|
}
|
|
if TokenValid(session, now) {
|
|
return cast(AuthResult)AuthResult.AUTH_OK
|
|
}
|
|
result := Login(client, session)
|
|
if result == cast(AuthResult)AuthResult.AUTH_OK {
|
|
SetExpiry(session, now)
|
|
}
|
|
return result
|
|
}
|
|
|
|
#scope_export
|
|
#program_export
|
|
ClearSession :: (session: *Session) {
|
|
if session != null {
|
|
session.* = Session.{}
|
|
}
|
|
}
|
|
|
|
// A 401 invalidates the bearer token, then performs exactly one fresh login
|
|
// and retries the original request. The caller persists the resulting session.
|
|
#scope_export
|
|
#program_export
|
|
BearerRequestWithHeaders :: (client: Client, session: *Session, now: s64,
|
|
method: string, path: string, body: string, output: []u8,
|
|
status: *s32, extra_headers: []HttpHeader) -> AuthResult {
|
|
if output.count == 0 || session == null || status == null {
|
|
return cast(AuthResult)AuthResult.AUTH_PAYLOAD_FAILED
|
|
}
|
|
output[0] = cast(u8)0
|
|
status.* = 0
|
|
if !MethodValid(method) || !PathValid(path) ||
|
|
extra_headers.count > 14 {
|
|
return cast(AuthResult)AuthResult.AUTH_PAYLOAD_FAILED
|
|
}
|
|
server: [512]u8
|
|
if !NormalizeUrl(client.server_url, server[:]) {
|
|
return cast(AuthResult)AuthResult.AUTH_INVALID_URL
|
|
}
|
|
url: [2048]u8
|
|
if !JoinUrl(TextUntilNul(server[:]), path, url[:]) {
|
|
return cast(AuthResult)AuthResult.AUTH_INVALID_URL
|
|
}
|
|
ready := EnsureSession(client, session, now)
|
|
if ready != cast(AuthResult)AuthResult.AUTH_OK {
|
|
return ready
|
|
}
|
|
attempt: s32 = 0
|
|
while attempt < 2 {
|
|
// The synchronous transport is finished before a rejected token is
|
|
// cleared or replaced. Keep its borrowed request inside that lifetime.
|
|
{
|
|
headers: [16]HttpHeader
|
|
headers[0].name = "X-Daochi-User"
|
|
headers[0].value = client.identity.account_id
|
|
headers[1].name = "X-Daochi-Client"
|
|
headers[1].value = client.identity.client_id
|
|
header_count: s64 = 2
|
|
index: s64 = 0
|
|
while index < cast(s64)extra_headers.count {
|
|
headers[header_count] = extra_headers[index]
|
|
header_count += 1
|
|
index += 1
|
|
}
|
|
request: HttpRequest
|
|
request.method = method
|
|
request.url = TextUntilNul(url[:])
|
|
request.token = TextUntilNul(session.token[:])
|
|
request.accept = "application/json"
|
|
request.headers = headers[0:header_count]
|
|
if body.count > 0 {
|
|
request.body = body
|
|
request.content_type = "application/json"
|
|
}
|
|
output[0] = cast(u8)0
|
|
status.* = 0
|
|
if !client.send(client.transport_context, request,
|
|
output, status) {
|
|
return cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED
|
|
}
|
|
}
|
|
if status.* >= 200 && status.* < 300 {
|
|
return cast(AuthResult)AuthResult.AUTH_OK
|
|
}
|
|
if status.* != 401 {
|
|
return cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED
|
|
}
|
|
ClearSession(session)
|
|
if attempt == 1 {
|
|
return cast(AuthResult)AuthResult.AUTH_FAILED
|
|
}
|
|
login_result := EnsureSession(client, session, now)
|
|
if login_result != cast(AuthResult)AuthResult.AUTH_OK {
|
|
return login_result
|
|
}
|
|
attempt += 1
|
|
}
|
|
return cast(AuthResult)AuthResult.AUTH_FAILED
|
|
}
|
|
|
|
#scope_export
|
|
#program_export
|
|
BearerRequest :: (client: Client, session: *Session, now: s64,
|
|
method: string, path: string, body: string, output: []u8,
|
|
status: *s32) -> AuthResult {
|
|
empty: [1]HttpHeader
|
|
return BearerRequestWithHeaders(client, session, now, method, path,
|
|
body, output, status, empty[0:0])
|
|
}
|