- Credit a Monero invoice in the same transaction that claims it, so two settlements that see different transaction sets cannot both credit it. - Add an optional signed manifest_sequence; a node and its mesh peers refuse an approved manifest older than the installed one, so a replayed approval cannot undo a suspension or key revocation. - Keep revoked device key IDs revoked instead of reactivating them on registration. - Take the rate-limit address from CF-Connecting-IP or the proxy's own X-Forwarded-For entry, never the client-chosen first entry, and key per-account challenge and login limits by account and address so strangers cannot lock an account out. - Keep a signed sync transaction spent once its writes commit. - Stop a paired peer widening its scope by omitting app, collection or space lists; cap mesh pull pages and peer response size. - Bound metric labels by route pattern, cap request headers at 64 KiB, check subprotocol WebSocket tokens against deleted accounts, match app record prefixes case-sensitively, compare the app admin token in constant time, require a node key's public half to match its seed, and stop printing invalid secrets or raw database errors. - Only render http and https peer links on the nodes page.
1077 lines
39 KiB
Go
1077 lines
39 KiB
Go
// Code generated by zi2go from authorization_http.zi. DO NOT EDIT.
|
|
package main
|
|
|
|
import (
|
|
sql "database/sql"
|
|
hex "encoding/hex"
|
|
strconv "strconv"
|
|
strings "strings"
|
|
)
|
|
|
|
// #import std_go_types
|
|
type Authorizations struct {
|
|
Database *Database
|
|
Configuration *Config
|
|
Counters *ServerMetrics
|
|
Verify VerifySignature
|
|
Replay Error
|
|
NodeID string
|
|
Limiter *RateLimiter
|
|
}
|
|
|
|
type RequestedScope struct {
|
|
Collection string "json:\"collection\""
|
|
Read bool "json:\"read\""
|
|
Write bool "json:\"write\""
|
|
}
|
|
|
|
type AuthorizationRequest struct {
|
|
AppID string "json:\"app_id\""
|
|
Telegram bool "json:\"telegram\""
|
|
Scopes []RequestedScope "json:\"scopes\""
|
|
}
|
|
|
|
type Rendezvous struct {
|
|
RequestID string "json:\"request_id\""
|
|
GrantID string "json:\"grant_id\""
|
|
AccountID string "json:\"account_id\""
|
|
AppID string "json:\"app_id\""
|
|
NodeID string "json:\"node_id\""
|
|
Audience string "json:\"audience\""
|
|
ExpiresAt int64 "json:\"expires_at\""
|
|
BotID int64 "json:\"bot_id\""
|
|
TelegramID int64 "json:\"telegram_id\""
|
|
ClientID string "json:\"client_id\""
|
|
SigningKey string "json:\"signing_key\""
|
|
EncryptionKey string "json:\"encryption_key\""
|
|
Challenge string "json:\"challenge\""
|
|
Status string "json:\"status\""
|
|
Scopes []RequestedScope "json:\"scopes\""
|
|
}
|
|
|
|
type RendezvousChallenge struct {
|
|
RequestID string "json:\"request_id\""
|
|
GrantID string "json:\"grant_id\""
|
|
AppID string "json:\"app_id\""
|
|
NodeID string "json:\"node_id\""
|
|
Audience string "json:\"audience\""
|
|
ExpiresAt int64 "json:\"expires_at\""
|
|
Challenge string "json:\"challenge\""
|
|
Status string "json:\"status\""
|
|
}
|
|
|
|
type RendezvousID struct {
|
|
RequestID string "json:\"request_id\""
|
|
AppID string "json:\"app_id\""
|
|
}
|
|
|
|
type RendezvousClaim struct {
|
|
EntryID string "json:\"entry_id,omitempty\""
|
|
RequestID string "json:\"request_id\""
|
|
ClientID string "json:\"client_id\""
|
|
SigningKey string "json:\"signing_key\""
|
|
EncryptionKey string "json:\"encryption_key\""
|
|
InitData string "json:\"init_data\""
|
|
Proof string "json:\"proof\""
|
|
}
|
|
|
|
type ClaimIdentity struct {
|
|
RequestID string "json:\"request_id\""
|
|
BotID int64 "json:\"bot_id\""
|
|
TelegramID int64 "json:\"telegram_id\""
|
|
InitDigest string "json:\"init_digest\""
|
|
NodeID string "json:\"node_id\""
|
|
Audience string "json:\"audience\""
|
|
}
|
|
|
|
type GrantIDRequest struct {
|
|
GrantID string "json:\"grant_id\""
|
|
AppID string "json:\"app_id\""
|
|
}
|
|
|
|
type SessionAnswer struct {
|
|
Credential SessionCredential "json:\"credential\""
|
|
Grant Grant "json:\"grant\""
|
|
OwnerPublicKey string "json:\"owner_public_key\""
|
|
}
|
|
|
|
func AuthorizationHttp_Failure(service Authorizations, writer ResponseWriter, status int, message string) {
|
|
Response_Error(writer, status, message)
|
|
}
|
|
|
|
func AuthorizationHttp_Guard(service Authorizations, writer ResponseWriter, request *Request) bool {
|
|
var value_0 [2]string
|
|
value_0[0] = "authorization:"
|
|
var value_1 string = ClientAddress_FromRequest(request)
|
|
value_0[1] = value_1
|
|
fields := value_0
|
|
_ = fields
|
|
var value_2 bool = RequestRate_Allow(service.Limiter, service.Counters, strings.Join(fields[0:2:2], ""), 120, Duration(60000000000))
|
|
if !value_2 {
|
|
Response_Error(writer, 429, "authorization rate limit exceeded")
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func AuthorizationHttp_Owner(service Authorizations, writer ResponseWriter, request *Request, raw []uint8, appID string) string {
|
|
account := HttpAuth_BearerUser(service.Database, request, service.Configuration.TokenSecret)
|
|
if account.Authentication.Error != nil || account.Authentication.Status != 0 {
|
|
HttpAuth_Respond(writer, service.Counters, account.Authentication)
|
|
return ""
|
|
}
|
|
header := SignedTx_ReadHeader(request)
|
|
if header.Authentication.Error != nil || header.Authentication.Status != 0 {
|
|
HttpAuth_Respond(writer, service.Counters, header.Authentication)
|
|
return ""
|
|
}
|
|
verified := SignedTx_Verify(service.Database, StdHttpGo_Context(request), request, raw, header.Value, account.Value, appID, service.Verify, service.Replay)
|
|
if verified.Error != nil || verified.Status != 0 {
|
|
HttpAuth_Respond(writer, service.Counters, verified)
|
|
return ""
|
|
}
|
|
return account.Value
|
|
}
|
|
|
|
func AuthorizationHttp_ReadRendezvous(transaction *Transaction, context Context, requestID string) Rendezvous {
|
|
var result Rendezvous = Rendezvous{}
|
|
var scopes string = ""
|
|
var value_0 [1]Any
|
|
value_0[0] = requestID
|
|
arguments := value_0
|
|
_ = arguments
|
|
var value_1 [14]Any
|
|
value_1[0] = &(result.RequestID)
|
|
value_1[1] = &(result.AccountID)
|
|
value_1[2] = &(result.AppID)
|
|
value_1[3] = &(result.NodeID)
|
|
value_1[4] = &(result.Audience)
|
|
value_1[5] = &(result.ExpiresAt)
|
|
value_1[6] = &(result.BotID)
|
|
value_1[7] = &(result.TelegramID)
|
|
value_1[8] = &(result.ClientID)
|
|
value_1[9] = &(result.SigningKey)
|
|
value_1[10] = &(result.EncryptionKey)
|
|
value_1[11] = &(result.Challenge)
|
|
value_1[12] = &(result.Status)
|
|
value_1[13] = &(scopes)
|
|
destinations := value_1
|
|
_ = destinations
|
|
var value_2 string = "SELECT request_id,account_id,app_id,node_id,audience,expires_at,bot_id,telegram_id,client_id,signing_key,encryption_key,claim_nonce,status,scopes_json FROM server_authorization_requests WHERE request_id=?1"
|
|
row := StdSqlGo_QueryRowTx(transaction, context, value_2, arguments[0:1:1])
|
|
var value_3 Error = StdSqlGo_ScanRow(row, destinations[0:14:14])
|
|
var value_4 bool = value_3 != nil
|
|
var value_5 bool = value_4
|
|
if !value_5 {
|
|
var value_6 Error = StdJsonGo_Unmarshal(StdTextGo_ToBytes(scopes), &(result.Scopes))
|
|
value_5 = (value_6 != nil)
|
|
}
|
|
if value_5 {
|
|
result.Status = "unavailable"
|
|
}
|
|
if result.Status == "approved" {
|
|
var value_7 [1]Any
|
|
value_7[0] = &(result.GrantID)
|
|
destinations := value_7
|
|
_ = destinations
|
|
row = StdSqlGo_QueryRowTx(transaction, context, "SELECT grant_id FROM server_authorization_grants WHERE request_id=?1", arguments[0:1:1])
|
|
var value_8 Error = StdSqlGo_ScanRow(row, destinations[0:1:1])
|
|
if value_8 != nil {
|
|
result.Status = "unavailable"
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func AuthorizationHttp_ClaimMessage(pending Rendezvous, claim RendezvousClaim, initDigest string) string {
|
|
var value_0 [12]string
|
|
value_0[0] = "daochi-rendezvous-claim-v1"
|
|
value_0[1] = pending.RequestID
|
|
value_0[2] = pending.AppID
|
|
value_0[3] = pending.NodeID
|
|
value_0[4] = pending.Audience
|
|
value_0[5] = pending.Challenge
|
|
var value_1 string = strconv.FormatInt(int64(pending.ExpiresAt), int(10))
|
|
value_0[6] = value_1
|
|
value_0[7] = claim.ClientID
|
|
value_0[8] = claim.SigningKey
|
|
value_0[9] = claim.EncryptionKey
|
|
value_0[10] = initDigest
|
|
value_0[11] = ""
|
|
fields := value_0
|
|
_ = fields
|
|
return strings.Join(fields[0:12:12], "\n")
|
|
}
|
|
|
|
func AuthorizationHttp_CreateRequest(service Authorizations, writer ResponseWriter, request *Request) {
|
|
var value_0 bool = AuthorizationHttp_Guard(service, writer, request)
|
|
if !value_0 {
|
|
return
|
|
}
|
|
raw := HttpBody_ReadJSON(writer, request, 8192)
|
|
var input AuthorizationRequest = AuthorizationRequest{}
|
|
var value_1 bool = raw.Error != nil
|
|
var value_2 bool = value_1
|
|
if !value_2 {
|
|
var value_3 Error = StrictJson_Decode(raw.Value, &(input))
|
|
value_2 = (value_3 != nil)
|
|
}
|
|
var value_4 bool = value_2
|
|
var value_5 bool = value_4
|
|
if !value_5 {
|
|
var value_6 bool = Identity_ValidNamespace(input.AppID)
|
|
value_5 = !value_6
|
|
}
|
|
if value_5 || int64(len(input.Scopes)) < 1 || int64(len(input.Scopes)) > 16 {
|
|
AuthorizationHttp_Failure(service, writer, 400, "invalid authorization request")
|
|
return
|
|
}
|
|
account := AuthorizationHttp_Owner(service, writer, request, raw.Value, input.AppID)
|
|
if account == "" {
|
|
return
|
|
}
|
|
var check SyncRequest = SyncRequest{}
|
|
check.AppID = input.AppID
|
|
check.ProtocolVersion = 6
|
|
var value_7 Error = SyncValidation_Validate(service.Database, StdHttpGo_Context(request), check)
|
|
if value_7 != nil {
|
|
AuthorizationHttp_Failure(service, writer, 403, "app unavailable")
|
|
return
|
|
}
|
|
previous := ""
|
|
{
|
|
value_8 := input.Scopes[:]
|
|
if int64(0) < 0 || int64(int64(len(value_8))) < int64(0) || int64(int64(len(value_8))) > int64(len(value_8)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
loop_view_21 := value_8[0:int64(len(value_8)):int64(len(value_8))]
|
|
loop_count_21 := int64(len(loop_view_21))
|
|
var loop_cursor_21 int64 = 0
|
|
for loop_cursor_21 < loop_count_21 {
|
|
loop_index_21 := loop_cursor_21
|
|
scope := loop_view_21[loop_index_21]
|
|
var value_9 bool = Identity_ValidEncryptedHierarchyCollection(scope.Collection)
|
|
var value_10 bool = !value_9 || int64(len(scope.Collection)) > 64
|
|
if !value_10 {
|
|
var value_11 bool = strings.HasPrefix(scope.Collection, "account.")
|
|
value_10 = value_11
|
|
}
|
|
var value_12 bool = value_10
|
|
if !value_12 {
|
|
var value_13 bool = Authorization_SafeCollection(input.AppID, scope.Collection)
|
|
value_12 = !value_13
|
|
}
|
|
var value_14 bool = value_12
|
|
if !value_14 {
|
|
var value_15 bool = strings.Compare(scope.Collection, previous) <= 0
|
|
value_14 = value_15
|
|
}
|
|
if value_14 || (!scope.Read && !scope.Write) {
|
|
AuthorizationHttp_Failure(service, writer, 400, "invalid authorization scopes")
|
|
return
|
|
}
|
|
previous = scope.Collection
|
|
loop_cursor_21++
|
|
}
|
|
}
|
|
var value_16 Rendezvous = Rendezvous{}
|
|
var value_17 string = TelegramTransport_Nonce()
|
|
value_16.RequestID = value_17
|
|
value_16.AccountID = account
|
|
value_16.AppID = input.AppID
|
|
value_16.NodeID = service.NodeID
|
|
value_16.Audience = service.Configuration.BaseURL
|
|
var value_18 int64 = StdTimeGo_Unix(StdTimeGo_Now()) + 600
|
|
value_16.ExpiresAt = value_18
|
|
var value_19 string = TelegramTransport_Nonce()
|
|
value_16.Challenge = value_19
|
|
value_16.Status = "pending"
|
|
value_16.Scopes = input.Scopes
|
|
result := value_16
|
|
if int64(len(result.RequestID)) != 32 || int64(len(result.Challenge)) != 32 || int64(len(service.NodeID)) != 64 {
|
|
AuthorizationHttp_Failure(service, writer, 503, "authorization unavailable")
|
|
return
|
|
}
|
|
if input.Telegram {
|
|
var value_20 int64 = TelegramInit_BotID(service.Configuration.LumiBotToken)
|
|
result.BotID = value_20
|
|
var value_21 [1]Any
|
|
value_21[0] = account
|
|
arguments := value_21
|
|
_ = arguments
|
|
var value_22 [1]Any
|
|
value_22[0] = &(result.TelegramID)
|
|
destinations := value_22
|
|
_ = destinations
|
|
var value_23 Context = StdHttpGo_Context(request)
|
|
row := StdSqlGo_QueryRow(service.Database, value_23, "SELECT telegram_id FROM server_lumi_telegram WHERE account_id=?1", arguments[0:1:1])
|
|
var value_24 bool = result.BotID == 0
|
|
var value_25 bool = value_24
|
|
if !value_25 {
|
|
var value_26 Error = StdSqlGo_ScanRow(row, destinations[0:1:1])
|
|
value_25 = (value_26 != nil)
|
|
}
|
|
if value_25 || result.TelegramID <= 0 {
|
|
AuthorizationHttp_Failure(service, writer, 409, "connect this Telegram chat before requesting Mini App access")
|
|
return
|
|
}
|
|
}
|
|
encoded := StdJsonGo_Marshal(input.Scopes)
|
|
if encoded.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "authorization unavailable")
|
|
return
|
|
}
|
|
var value_27 [10]Any
|
|
value_27[0] = result.RequestID
|
|
value_27[1] = account
|
|
value_27[2] = input.AppID
|
|
value_27[3] = service.NodeID
|
|
value_27[4] = result.Audience
|
|
value_27[5] = result.ExpiresAt
|
|
value_27[6] = result.BotID
|
|
value_27[7] = result.TelegramID
|
|
value_27[8] = result.Challenge
|
|
var value_28 string = StdTextGo_FromBytes(encoded.Value)
|
|
value_27[9] = value_28
|
|
arguments := value_27
|
|
_ = arguments
|
|
var value_29 Context = StdHttpGo_Context(request)
|
|
var value_30 string = "INSERT INTO server_authorization_requests(request_id,account_id,app_id,node_id,audience,expires_at,bot_id,telegram_id,claim_nonce,scopes_json) VALUES(?1,?2,?3,?4,?5,?6,?7,?8,?9,?10)"
|
|
written := StdSqlGo_Exec(service.Database, value_29, value_30, arguments[0:10:10])
|
|
if written.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "authorization unavailable")
|
|
return
|
|
}
|
|
Response_JSON(writer, 200, result)
|
|
}
|
|
|
|
func AuthorizationHttp_RendezvousStatus(service Authorizations, writer ResponseWriter, request *Request, cancel bool) {
|
|
var value_0 bool = AuthorizationHttp_Guard(service, writer, request)
|
|
if !value_0 {
|
|
return
|
|
}
|
|
raw := HttpBody_ReadJSON(writer, request, 1024)
|
|
var input RendezvousID = RendezvousID{}
|
|
var value_1 bool = raw.Error != nil
|
|
var value_2 bool = value_1
|
|
if !value_2 {
|
|
var value_3 Error = StrictJson_Decode(raw.Value, &(input))
|
|
value_2 = (value_3 != nil)
|
|
}
|
|
var value_4 bool = value_2
|
|
var value_5 bool = value_4
|
|
if !value_5 {
|
|
var value_6 bool = Authorization_HexID(input.RequestID, 32)
|
|
value_5 = !value_6
|
|
}
|
|
if value_5 {
|
|
AuthorizationHttp_Failure(service, writer, 400, "invalid rendezvous")
|
|
return
|
|
}
|
|
account := ""
|
|
if cancel {
|
|
account = AuthorizationHttp_Owner(service, writer, request, raw.Value, input.AppID)
|
|
if account == "" {
|
|
return
|
|
}
|
|
} else if StdHttpGo_HeaderValue(StdHttpGo_Headers(request), "Authorization") != "" {
|
|
owner := HttpAuth_BearerUser(service.Database, request, service.Configuration.TokenSecret)
|
|
if owner.Authentication.Error != nil || owner.Authentication.Status != 0 {
|
|
HttpAuth_Respond(writer, service.Counters, owner.Authentication)
|
|
return
|
|
}
|
|
account = owner.Value
|
|
}
|
|
var value_7 Context = StdHttpGo_Context(request)
|
|
opened := StdSqlGo_Begin(service.Database, value_7, nil)
|
|
if opened.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "rendezvous unavailable")
|
|
return
|
|
}
|
|
defer (*sql.Tx).Rollback(opened.Value)
|
|
if cancel {
|
|
var value_8 [3]Any
|
|
value_8[0] = input.RequestID
|
|
value_8[1] = account
|
|
value_8[2] = input.AppID
|
|
arguments := value_8
|
|
_ = arguments
|
|
var value_9 Context = StdHttpGo_Context(request)
|
|
var value_10 string = "UPDATE server_authorization_requests SET status='cancelled' WHERE request_id=?1 AND account_id=?2 AND app_id=?3 AND status IN ('pending','claimed')"
|
|
written := StdSqlGo_ExecTx(opened.Value, value_9, value_10, arguments[0:3:3])
|
|
if written.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "rendezvous unavailable")
|
|
return
|
|
}
|
|
changed := StdSqlGo_RowsAffected(written.Value)
|
|
if changed.Error != nil || changed.Value != 1 {
|
|
AuthorizationHttp_Failure(service, writer, 409, "rendezvous cannot be cancelled")
|
|
return
|
|
}
|
|
}
|
|
var value_11 Context = StdHttpGo_Context(request)
|
|
result := AuthorizationHttp_ReadRendezvous(opened.Value, value_11, input.RequestID)
|
|
var value_12 bool = result.NodeID != service.NodeID || result.Audience != service.Configuration.BaseURL || result.Status == "unavailable"
|
|
var value_13 bool = value_12
|
|
var value_14 bool = value_13
|
|
if !value_14 {
|
|
var value_15 bool = !cancel
|
|
if value_15 {
|
|
var value_16 bool = result.ExpiresAt <= StdTimeGo_Unix(StdTimeGo_Now())
|
|
value_15 = value_16
|
|
}
|
|
value_14 = value_15
|
|
}
|
|
if value_14 {
|
|
AuthorizationHttp_Failure(service, writer, 410, "rendezvous unavailable or expired")
|
|
return
|
|
}
|
|
if account != "" && account != result.AccountID {
|
|
AuthorizationHttp_Failure(service, writer, 404, "owned rendezvous not found")
|
|
return
|
|
}
|
|
var value_17 Error = StdSqlGo_Commit(opened.Value)
|
|
if value_17 != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "rendezvous unavailable")
|
|
return
|
|
}
|
|
if account != "" {
|
|
Response_JSON(writer, 200, result)
|
|
} else {
|
|
public := RendezvousChallenge{RequestID: result.RequestID, GrantID: result.GrantID, AppID: result.AppID, NodeID: result.NodeID, Audience: result.Audience, ExpiresAt: result.ExpiresAt, Challenge: result.Challenge, Status: result.Status}
|
|
Response_JSON(writer, 200, public)
|
|
}
|
|
}
|
|
|
|
func AuthorizationHttp_Claim(service Authorizations, writer ResponseWriter, request *Request) {
|
|
var value_0 bool = AuthorizationHttp_Guard(service, writer, request)
|
|
if !value_0 {
|
|
return
|
|
}
|
|
raw := HttpBody_ReadJSON(writer, request, 24576)
|
|
var input RendezvousClaim = RendezvousClaim{}
|
|
var value_1 bool = raw.Error != nil
|
|
var value_2 bool = value_1
|
|
if !value_2 {
|
|
var value_3 Error = StrictJson_Decode(raw.Value, &(input))
|
|
value_2 = (value_3 != nil)
|
|
}
|
|
var value_4 bool = value_2
|
|
var value_5 bool = value_4
|
|
if !value_5 {
|
|
var value_6 bool = Authorization_HexID(input.RequestID, 32)
|
|
value_5 = !value_6
|
|
}
|
|
var value_7 bool = value_5
|
|
var value_8 bool = value_7
|
|
if !value_8 {
|
|
var value_9 bool = Authorization_HexID(input.ClientID, 64)
|
|
value_8 = !value_9
|
|
}
|
|
var value_10 bool = value_8
|
|
var value_11 bool = value_10
|
|
if !value_11 {
|
|
var value_12 bool = Authorization_HexID(input.SigningKey, 64)
|
|
value_11 = !value_12
|
|
}
|
|
var value_13 bool = value_11
|
|
var value_14 bool = value_13
|
|
if !value_14 {
|
|
var value_15 bool = Authorization_HexID(input.EncryptionKey, 2368)
|
|
value_14 = !value_15
|
|
}
|
|
var value_16 bool = value_14 || input.SigningKey == input.EncryptionKey
|
|
var value_17 bool = value_16
|
|
if !value_17 {
|
|
var value_18 bool = input.EntryID != ""
|
|
var value_19 bool = value_18
|
|
if value_19 {
|
|
var value_20 bool = Authorization_HexID(input.EntryID, 32)
|
|
value_19 = (!value_20 || input.InitData != "")
|
|
}
|
|
value_17 = value_19
|
|
}
|
|
if value_17 {
|
|
AuthorizationHttp_Failure(service, writer, 400, "invalid delegate claim")
|
|
return
|
|
}
|
|
context := StdHttpGo_Context(request)
|
|
now := StdTimeGo_Unix(StdTimeGo_Now())
|
|
opened := StdSqlGo_Begin(service.Database, context, nil)
|
|
if opened.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "rendezvous unavailable")
|
|
return
|
|
}
|
|
transaction := opened.Value
|
|
defer (*sql.Tx).Rollback(transaction)
|
|
var value_21 [5]Any
|
|
value_21[0] = input.RequestID
|
|
value_21[1] = input.ClientID
|
|
value_21[2] = input.SigningKey
|
|
value_21[3] = input.EncryptionKey
|
|
value_21[4] = now
|
|
arguments := value_21
|
|
_ = arguments
|
|
locked := StdSqlGo_ExecTx(transaction, context, "UPDATE server_authorization_requests SET request_id=request_id WHERE request_id=?1", arguments[0:1:1])
|
|
if locked.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "rendezvous unavailable")
|
|
return
|
|
}
|
|
pending := AuthorizationHttp_ReadRendezvous(transaction, context, input.RequestID)
|
|
if pending.Status != "pending" || pending.ExpiresAt <= now || pending.NodeID != service.NodeID || pending.Audience != service.Configuration.BaseURL {
|
|
AuthorizationHttp_Failure(service, writer, 409, "rendezvous expired, cancelled or already claimed")
|
|
return
|
|
}
|
|
digest := ""
|
|
if pending.BotID > 0 {
|
|
if input.EntryID != "" {
|
|
var value_22 [1]Any
|
|
value_22[0] = input.EntryID
|
|
entryArgument := value_22
|
|
_ = entryArgument
|
|
var value_23 [1]Any
|
|
value_23[0] = &(digest)
|
|
entryDestination := value_23
|
|
_ = entryDestination
|
|
row := StdSqlGo_QueryRowTx(transaction, context, "SELECT init_digest FROM server_telegram_account_entries WHERE entry_id=?1", entryArgument[0:1:1])
|
|
_ = row
|
|
var value_24 Error = StdSqlGo_ScanRow(row, entryDestination[0:1:1])
|
|
var value_25 bool = value_24 != nil
|
|
var value_26 bool = value_25
|
|
if !value_26 {
|
|
var value_27 bool = Authorization_HexID(digest, 64)
|
|
value_26 = !value_27
|
|
}
|
|
var value_28 bool = value_26
|
|
var value_29 bool = value_28
|
|
if !value_29 {
|
|
var value_30 bool = TelegramAccountEntry_ClaimTx(transaction, context, input.EntryID, pending.AccountID, pending.BotID, pending.TelegramID, input.ClientID, input.SigningKey, input.EncryptionKey, pending.NodeID, pending.Audience, now)
|
|
value_29 = !value_30
|
|
}
|
|
if value_29 {
|
|
AuthorizationHttp_Failure(service, writer, 409, "verified Telegram entry does not match this request")
|
|
return
|
|
}
|
|
} else {
|
|
verified := TelegramInit_Validate(input.InitData, service.Configuration.LumiBotToken, now)
|
|
if !verified.Valid || verified.BotID != pending.BotID || verified.UserID != pending.TelegramID {
|
|
AuthorizationHttp_Failure(service, writer, 401, "verified Telegram sender required")
|
|
return
|
|
}
|
|
digest = verified.Digest
|
|
}
|
|
var matches int64 = 0
|
|
var value_31 [1]Any
|
|
value_31[0] = &(matches)
|
|
destinations := value_31
|
|
_ = destinations
|
|
var value_32 [2]Any
|
|
value_32[0] = pending.AccountID
|
|
value_32[1] = pending.TelegramID
|
|
binding := value_32
|
|
_ = binding
|
|
row := StdSqlGo_QueryRowTx(transaction, context, "SELECT COUNT(*) FROM server_lumi_telegram WHERE account_id=?1 AND telegram_id=?2", binding[0:2:2])
|
|
_ = row
|
|
var value_33 Error = StdSqlGo_ScanRow(row, destinations[0:1:1])
|
|
if value_33 != nil || matches != 1 {
|
|
AuthorizationHttp_Failure(service, writer, 409, "Telegram connection changed")
|
|
return
|
|
}
|
|
} else if input.InitData != "" || input.EntryID != "" {
|
|
AuthorizationHttp_Failure(service, writer, 400, "unexpected Telegram claim")
|
|
return
|
|
}
|
|
var value_34 string = input.SigningKey
|
|
var value_35 string = AuthorizationHttp_ClaimMessage(pending, input, digest)
|
|
var value_36 bool = Session_VerifyKeyProof(value_34, value_35, input.Proof)
|
|
if !value_36 {
|
|
AuthorizationHttp_Failure(service, writer, 401, "delegate claim proof rejected")
|
|
return
|
|
}
|
|
if digest != "" && input.EntryID == "" {
|
|
var value_37 [1]Any
|
|
value_37[0] = now
|
|
cutoff := value_37
|
|
_ = cutoff
|
|
pruned := StdSqlGo_ExecTx(transaction, context, "DELETE FROM server_telegram_init_claims WHERE expires_at<?1", cutoff[0:1:1])
|
|
if pruned.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "claim unavailable")
|
|
return
|
|
}
|
|
pruned = StdSqlGo_ExecTx(transaction, context, "DELETE FROM server_telegram_init_replays WHERE expires_at<?1", cutoff[0:1:1])
|
|
var value_38 [2]Any
|
|
value_38[0] = digest
|
|
value_38[1] = now + 300
|
|
globalReplay := value_38
|
|
_ = globalReplay
|
|
var value_39 string = "INSERT INTO server_telegram_init_replays(digest,expires_at) VALUES(?1,?2) ON CONFLICT(digest) DO NOTHING"
|
|
globalClaim := StdSqlGo_ExecTx(transaction, context, value_39, globalReplay[0:2:2])
|
|
if pruned.Error != nil || globalClaim.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "claim unavailable")
|
|
return
|
|
}
|
|
globalChanged := StdSqlGo_RowsAffected(globalClaim.Value)
|
|
if globalChanged.Error != nil || globalChanged.Value != 1 {
|
|
AuthorizationHttp_Failure(service, writer, 409, "Telegram initData already claimed")
|
|
return
|
|
}
|
|
var value_40 [3]Any
|
|
value_40[0] = digest
|
|
value_40[1] = input.RequestID
|
|
value_40[2] = now + 300
|
|
replay := value_40
|
|
_ = replay
|
|
var value_41 string = "INSERT INTO server_telegram_init_claims(digest,request_id,expires_at) VALUES(?1,?2,?3) ON CONFLICT(digest) DO NOTHING"
|
|
claimed := StdSqlGo_ExecTx(transaction, context, value_41, replay[0:3:3])
|
|
if claimed.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "claim unavailable")
|
|
return
|
|
}
|
|
changed := StdSqlGo_RowsAffected(claimed.Value)
|
|
_ = changed
|
|
if changed.Error != nil || changed.Value != 1 {
|
|
AuthorizationHttp_Failure(service, writer, 409, "Telegram initData already claimed")
|
|
return
|
|
}
|
|
}
|
|
var value_42 string = "UPDATE server_authorization_requests SET status='claimed',client_id=?2,signing_key=?3,encryption_key=?4 WHERE request_id=?1 AND status='pending' AND expires_at>?5"
|
|
written := StdSqlGo_ExecTx(transaction, context, value_42, arguments[0:5:5])
|
|
if written.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "claim unavailable")
|
|
return
|
|
}
|
|
changed := StdSqlGo_RowsAffected(written.Value)
|
|
_ = changed
|
|
var value_43 bool = changed.Error != nil || changed.Value != 1
|
|
var value_44 bool = value_43
|
|
if !value_44 {
|
|
var value_45 Error = StdSqlGo_Commit(transaction)
|
|
value_44 = (value_45 != nil)
|
|
}
|
|
if value_44 {
|
|
AuthorizationHttp_Failure(service, writer, 409, "claim unavailable")
|
|
return
|
|
}
|
|
result := ClaimIdentity{RequestID: pending.RequestID, BotID: pending.BotID, TelegramID: pending.TelegramID, InitDigest: digest, NodeID: pending.NodeID, Audience: pending.Audience}
|
|
Response_JSON(writer, 200, result)
|
|
}
|
|
|
|
func AuthorizationHttp_Approve(service Authorizations, writer ResponseWriter, request *Request) {
|
|
var value_0 bool = AuthorizationHttp_Guard(service, writer, request)
|
|
if !value_0 {
|
|
return
|
|
}
|
|
raw := HttpBody_ReadJSON(writer, request, 262144)
|
|
var grant Grant = Grant{}
|
|
var value_1 bool = raw.Error != nil
|
|
var value_2 bool = value_1
|
|
if !value_2 {
|
|
var value_3 Error = StrictJson_Decode(raw.Value, &(grant))
|
|
value_2 = (value_3 != nil)
|
|
}
|
|
if value_2 {
|
|
AuthorizationHttp_Failure(service, writer, 400, "invalid authorization grant")
|
|
return
|
|
}
|
|
owner := HttpAuth_BearerUser(service.Database, request, service.Configuration.TokenSecret)
|
|
if owner.Authentication.Error != nil || owner.Authentication.Status != 0 {
|
|
HttpAuth_Respond(writer, service.Counters, owner.Authentication)
|
|
return
|
|
}
|
|
var value_4 Context = StdHttpGo_Context(request)
|
|
verified := Authorization_VerifyGrant(service.Database, value_4, grant, owner.Value, service.NodeID, service.Configuration.BaseURL, service.Verify)
|
|
if verified.Error != nil || verified.Status != 0 {
|
|
HttpAuth_Respond(writer, service.Counters, verified)
|
|
return
|
|
}
|
|
var value_5 Context = StdHttpGo_Context(request)
|
|
opened := StdSqlGo_Begin(service.Database, value_5, nil)
|
|
if opened.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "approval unavailable")
|
|
return
|
|
}
|
|
transaction := opened.Value
|
|
defer (*sql.Tx).Rollback(transaction)
|
|
var value_6 [10]Any
|
|
value_6[0] = grant.GrantID
|
|
value_6[1] = grant.AccountID
|
|
value_6[2] = grant.RequestID
|
|
value_6[3] = grant.AppID
|
|
value_6[4] = grant.NodeID
|
|
value_6[5] = grant.Audience
|
|
value_6[6] = grant.NotBefore
|
|
value_6[7] = grant.ExpiresAt
|
|
value_6[8] = grant.Nonce
|
|
var value_7 string = StdTextGo_FromBytes(raw.Value)
|
|
value_6[9] = value_7
|
|
arguments := value_6
|
|
_ = arguments
|
|
var value_8 [2]Any
|
|
value_8[0] = grant.RequestID
|
|
var value_9 int64 = StdTimeGo_Unix(StdTimeGo_Now())
|
|
value_8[1] = value_9
|
|
claim := value_8
|
|
_ = claim
|
|
var value_10 Context = StdHttpGo_Context(request)
|
|
var value_11 string = "UPDATE server_authorization_requests SET status='approved' WHERE request_id=?1 AND status='claimed' AND expires_at>?2"
|
|
written := StdSqlGo_ExecTx(transaction, value_10, value_11, claim[0:2:2])
|
|
if written.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "approval unavailable")
|
|
return
|
|
}
|
|
changed := StdSqlGo_RowsAffected(written.Value)
|
|
var value_12 Context = StdHttpGo_Context(request)
|
|
pending := AuthorizationHttp_ReadRendezvous(transaction, value_12, grant.RequestID)
|
|
var value_13 bool = changed.Error != nil || changed.Value != 1 || pending.AccountID != grant.AccountID || pending.AppID != grant.AppID
|
|
var value_14 bool = value_13 || pending.NodeID != grant.NodeID || pending.Audience != grant.Audience || pending.ClientID != grant.ClientID
|
|
var value_15 bool = value_14 || pending.SigningKey != grant.SigningKey || pending.EncryptionKey != grant.EncryptionKey
|
|
if value_15 || pending.BotID != grant.BotID || pending.TelegramID != grant.TelegramID || int64(len(pending.Scopes)) != int64(len(grant.Scopes)) {
|
|
AuthorizationHttp_Failure(service, writer, 409, "approval does not match verified delegate request")
|
|
return
|
|
}
|
|
{
|
|
var range_first_39 int64 = 0
|
|
range_last_39 := int64(len(grant.Scopes)) - 1
|
|
index := range_first_39
|
|
for index <= range_last_39 {
|
|
expected := pending.Scopes[index]
|
|
approved := grant.Scopes[index]
|
|
if expected.Collection != approved.Collection || expected.Read != approved.Read || expected.Write != approved.Write {
|
|
AuthorizationHttp_Failure(service, writer, 403, "approval scope mismatch")
|
|
return
|
|
}
|
|
if index == range_last_39 {
|
|
break
|
|
}
|
|
index++
|
|
}
|
|
}
|
|
var value_16 Context = StdHttpGo_Context(request)
|
|
var value_17 string = "INSERT INTO server_authorization_grants(grant_id,account_id,request_id,app_id,node_id,audience,not_before,expires_at,nonce,grant_json) VALUES(?1,?2,?3,?4,?5,?6,?7,?8,?9,?10)"
|
|
written = StdSqlGo_ExecTx(transaction, value_16, value_17, arguments[0:10:10])
|
|
if written.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 409, "authorization grant already issued")
|
|
return
|
|
}
|
|
var value_18 [2]Any
|
|
value_18[0] = grant.AccountID
|
|
value_18[1] = grant.ClientID
|
|
registration := value_18
|
|
_ = registration
|
|
var value_19 Context = StdHttpGo_Context(request)
|
|
var value_20 string = "INSERT INTO server_clients(user_id_hash,client_id,protocol_version) VALUES(?1,?2,6) ON CONFLICT(user_id_hash,client_id) DO UPDATE SET protocol_version=6"
|
|
written = StdSqlGo_ExecTx(transaction, value_19, value_20, registration[0:2:2])
|
|
if written.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "delegate client registration unavailable")
|
|
return
|
|
}
|
|
var value_21 Context = StdHttpGo_Context(request)
|
|
var value_22 string = grant.GrantID
|
|
var value_23 string = service.Configuration.BaseURL
|
|
active := Authorization_ActiveGrant(transaction, value_21, value_22, service.NodeID, value_23, StdTimeGo_Unix(StdTimeGo_Now()))
|
|
if active.Authentication.Error != nil || active.Authentication.Status != 0 {
|
|
HttpAuth_Respond(writer, service.Counters, active.Authentication)
|
|
return
|
|
}
|
|
var value_24 Error = StdSqlGo_Commit(transaction)
|
|
if value_24 != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "approval unavailable")
|
|
return
|
|
}
|
|
Response_JSON(writer, 200, true)
|
|
}
|
|
|
|
func AuthorizationHttp_Revoke(service Authorizations, writer ResponseWriter, request *Request) {
|
|
var value_0 bool = AuthorizationHttp_Guard(service, writer, request)
|
|
if !value_0 {
|
|
return
|
|
}
|
|
raw := HttpBody_ReadJSON(writer, request, 1024)
|
|
var input GrantIDRequest = GrantIDRequest{}
|
|
var value_1 bool = raw.Error != nil
|
|
var value_2 bool = value_1
|
|
if !value_2 {
|
|
var value_3 Error = StrictJson_Decode(raw.Value, &(input))
|
|
value_2 = (value_3 != nil)
|
|
}
|
|
var value_4 bool = value_2
|
|
var value_5 bool = value_4
|
|
if !value_5 {
|
|
var value_6 bool = Authorization_HexID(input.GrantID, 32)
|
|
value_5 = !value_6
|
|
}
|
|
if value_5 {
|
|
AuthorizationHttp_Failure(service, writer, 400, "invalid grant revocation")
|
|
return
|
|
}
|
|
owner := AuthorizationHttp_Owner(service, writer, request, raw.Value, input.AppID)
|
|
if owner == "" {
|
|
return
|
|
}
|
|
var value_7 [4]Any
|
|
value_7[0] = input.GrantID
|
|
value_7[1] = owner
|
|
value_7[2] = input.AppID
|
|
var value_8 int64 = StdTimeGo_Unix(StdTimeGo_Now())
|
|
value_7[3] = value_8
|
|
arguments := value_7
|
|
_ = arguments
|
|
var value_9 Context = StdHttpGo_Context(request)
|
|
var value_10 string = "UPDATE server_authorization_grants SET revoked_at=CASE WHEN revoked_at=0 THEN ?4 ELSE revoked_at END WHERE grant_id=?1 AND account_id=?2 AND app_id=?3"
|
|
written := StdSqlGo_Exec(service.Database, value_9, value_10, arguments[0:4:4])
|
|
if written.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "revocation unavailable")
|
|
return
|
|
}
|
|
changed := StdSqlGo_RowsAffected(written.Value)
|
|
if changed.Error != nil || changed.Value != 1 {
|
|
AuthorizationHttp_Failure(service, writer, 404, "authorization grant not found")
|
|
return
|
|
}
|
|
Response_JSON(writer, 200, true)
|
|
}
|
|
|
|
func AuthorizationHttp_Challenge(service Authorizations, writer ResponseWriter, request *Request) {
|
|
var value_0 bool = AuthorizationHttp_Guard(service, writer, request)
|
|
if !value_0 {
|
|
return
|
|
}
|
|
raw := HttpBody_ReadJSON(writer, request, 1024)
|
|
var input GrantIDRequest = GrantIDRequest{}
|
|
var value_1 bool = raw.Error != nil
|
|
var value_2 bool = value_1
|
|
if !value_2 {
|
|
var value_3 Error = StrictJson_Decode(raw.Value, &(input))
|
|
value_2 = (value_3 != nil)
|
|
}
|
|
var value_4 bool = value_2
|
|
var value_5 bool = value_4
|
|
if !value_5 {
|
|
var value_6 bool = Authorization_HexID(input.GrantID, 32)
|
|
value_5 = !value_6
|
|
}
|
|
if value_5 {
|
|
AuthorizationHttp_Failure(service, writer, 400, "invalid session challenge request")
|
|
return
|
|
}
|
|
now := StdTimeGo_Unix(StdTimeGo_Now())
|
|
var value_7 Context = StdHttpGo_Context(request)
|
|
opened := StdSqlGo_Begin(service.Database, value_7, nil)
|
|
if opened.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "session challenge unavailable")
|
|
return
|
|
}
|
|
transaction := opened.Value
|
|
defer (*sql.Tx).Rollback(transaction)
|
|
var value_8 [1]Any
|
|
value_8[0] = input.GrantID
|
|
lock := value_8
|
|
_ = lock
|
|
var value_9 Context = StdHttpGo_Context(request)
|
|
locked := StdSqlGo_ExecTx(transaction, value_9, "UPDATE server_authorization_grants SET grant_id=grant_id WHERE grant_id=?1", lock[0:1:1])
|
|
if locked.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "session challenge unavailable")
|
|
return
|
|
}
|
|
var value_10 Context = StdHttpGo_Context(request)
|
|
grant := Authorization_ActiveGrant(transaction, value_10, input.GrantID, service.NodeID, service.Configuration.BaseURL, now)
|
|
if grant.Authentication.Error != nil || grant.Authentication.Status != 0 {
|
|
HttpAuth_Respond(writer, service.Counters, grant.Authentication)
|
|
return
|
|
}
|
|
var value_11 SessionChallenge = SessionChallenge{}
|
|
value_11.GrantID = input.GrantID
|
|
var value_12 string = TelegramTransport_Nonce()
|
|
value_11.Challenge = value_12
|
|
value_11.NodeID = service.NodeID
|
|
value_11.Audience = service.Configuration.BaseURL
|
|
value_11.ExpiresAt = now + 90
|
|
result := value_11
|
|
if int64(len(result.Challenge)) != 32 {
|
|
AuthorizationHttp_Failure(service, writer, 503, "session challenge unavailable")
|
|
return
|
|
}
|
|
var value_13 [3]Any
|
|
value_13[0] = result.Challenge
|
|
value_13[1] = result.GrantID
|
|
value_13[2] = result.ExpiresAt
|
|
arguments := value_13
|
|
_ = arguments
|
|
var value_14 [1]Any
|
|
value_14[0] = now
|
|
cutoff := value_14
|
|
_ = cutoff
|
|
var value_15 Context = StdHttpGo_Context(request)
|
|
pruned := StdSqlGo_ExecTx(transaction, value_15, "DELETE FROM server_authorization_challenges WHERE expires_at<?1", cutoff[0:1:1])
|
|
if pruned.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "session challenge unavailable")
|
|
return
|
|
}
|
|
var value_16 Context = StdHttpGo_Context(request)
|
|
written := StdSqlGo_ExecTx(transaction, value_16, "INSERT INTO server_authorization_challenges(challenge,grant_id,expires_at) VALUES(?1,?2,?3)", arguments[0:3:3])
|
|
var value_17 bool = written.Error != nil
|
|
var value_18 bool = value_17
|
|
if !value_18 {
|
|
var value_19 Error = StdSqlGo_Commit(transaction)
|
|
value_18 = (value_19 != nil)
|
|
}
|
|
if value_18 {
|
|
AuthorizationHttp_Failure(service, writer, 503, "session challenge unavailable")
|
|
return
|
|
}
|
|
Response_JSON(writer, 200, result)
|
|
}
|
|
|
|
func AuthorizationHttp_IssueSession(service Authorizations, writer ResponseWriter, request *Request) {
|
|
var value_0 bool = AuthorizationHttp_Guard(service, writer, request)
|
|
if !value_0 {
|
|
return
|
|
}
|
|
raw := HttpBody_ReadJSON(writer, request, 2048)
|
|
var input SessionRequest = SessionRequest{}
|
|
var value_1 bool = raw.Error != nil
|
|
var value_2 bool = value_1
|
|
if !value_2 {
|
|
var value_3 Error = StrictJson_Decode(raw.Value, &(input))
|
|
value_2 = (value_3 != nil)
|
|
}
|
|
var value_4 bool = value_2
|
|
var value_5 bool = value_4
|
|
if !value_5 {
|
|
var value_6 bool = Authorization_HexID(input.GrantID, 32)
|
|
value_5 = !value_6
|
|
}
|
|
var value_7 bool = value_5
|
|
var value_8 bool = value_7
|
|
if !value_8 {
|
|
var value_9 bool = Authorization_HexID(input.Challenge, 32)
|
|
value_8 = !value_9
|
|
}
|
|
if value_8 {
|
|
AuthorizationHttp_Failure(service, writer, 400, "invalid session request")
|
|
return
|
|
}
|
|
now := StdTimeGo_Unix(StdTimeGo_Now())
|
|
context := StdHttpGo_Context(request)
|
|
opened := StdSqlGo_Begin(service.Database, context, nil)
|
|
if opened.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "session unavailable")
|
|
return
|
|
}
|
|
transaction := opened.Value
|
|
defer (*sql.Tx).Rollback(transaction)
|
|
var value_10 [3]Any
|
|
value_10[0] = input.Challenge
|
|
value_10[1] = input.GrantID
|
|
value_10[2] = now
|
|
arguments := value_10
|
|
_ = arguments
|
|
var value_11 string = "UPDATE server_authorization_challenges SET consumed_at=?3 WHERE challenge=?1 AND grant_id=?2 AND consumed_at=0 AND expires_at>?3"
|
|
claimed := StdSqlGo_ExecTx(transaction, context, value_11, arguments[0:3:3])
|
|
if claimed.Error != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "session unavailable")
|
|
return
|
|
}
|
|
changed := StdSqlGo_RowsAffected(claimed.Value)
|
|
if changed.Error != nil || changed.Value != 1 {
|
|
AuthorizationHttp_Failure(service, writer, 409, "session challenge expired or reused")
|
|
return
|
|
}
|
|
grant := Authorization_ActiveGrant(transaction, context, input.GrantID, service.NodeID, service.Configuration.BaseURL, now)
|
|
if grant.Authentication.Error != nil || grant.Authentication.Status != 0 {
|
|
HttpAuth_Respond(writer, service.Counters, grant.Authentication)
|
|
return
|
|
}
|
|
challenge := SessionChallenge{GrantID: input.GrantID, Challenge: input.Challenge, NodeID: service.NodeID, Audience: service.Configuration.BaseURL}
|
|
var value_12 [1]Any
|
|
value_12[0] = &(challenge.ExpiresAt)
|
|
destinations := value_12
|
|
_ = destinations
|
|
row := StdSqlGo_QueryRowTx(transaction, context, "SELECT expires_at FROM server_authorization_challenges WHERE challenge=?1 AND grant_id=?2", arguments[0:2:2])
|
|
var value_13 Error = StdSqlGo_ScanRow(row, destinations[0:1:1])
|
|
var value_14 bool = value_13 != nil
|
|
var value_15 bool = value_14
|
|
if !value_15 {
|
|
var value_16 string = Session_ChallengeMessage(challenge)
|
|
var value_17 bool = Session_VerifyKeyProof(grant.Value.SigningKey, value_16, input.Signature)
|
|
value_15 = !value_17
|
|
}
|
|
if value_15 {
|
|
AuthorizationHttp_Failure(service, writer, 401, "session key proof rejected")
|
|
return
|
|
}
|
|
var result SessionAnswer = SessionAnswer{}
|
|
result.Grant = grant.Value
|
|
var ownerKey []uint8 = nil
|
|
var value_18 [1]Any
|
|
value_18[0] = grant.Value.AccountID
|
|
ownerArguments := value_18
|
|
_ = ownerArguments
|
|
var value_19 [1]Any
|
|
value_19[0] = &(ownerKey)
|
|
ownerDestinations := value_19
|
|
_ = ownerDestinations
|
|
row = StdSqlGo_QueryRowTx(transaction, context, "SELECT public_key FROM server_users WHERE user_id_hash=?1", ownerArguments[0:1:1])
|
|
var value_20 Error = StdSqlGo_ScanRow(row, ownerDestinations[0:1:1])
|
|
if value_20 != nil {
|
|
AuthorizationHttp_Failure(service, writer, 503, "session unavailable")
|
|
return
|
|
}
|
|
result.OwnerPublicKey = hex.EncodeToString(ownerKey)
|
|
var value_21 SessionCredential = SessionCredential{}
|
|
var value_22 string = TelegramTransport_Nonce()
|
|
value_21.SessionID = value_22
|
|
value_21.GrantID = input.GrantID
|
|
var value_23 string = TelegramTransport_Nonce()
|
|
value_21.Challenge = value_23
|
|
value_21.ExpiresAt = now + 300
|
|
result.Credential = value_21
|
|
if result.Credential.ExpiresAt > grant.Value.ExpiresAt {
|
|
result.Credential.ExpiresAt = grant.Value.ExpiresAt
|
|
}
|
|
if int64(len(result.Credential.SessionID)) != 32 || int64(len(result.Credential.Challenge)) != 32 {
|
|
AuthorizationHttp_Failure(service, writer, 503, "session unavailable")
|
|
return
|
|
}
|
|
var value_24 [4]Any
|
|
value_24[0] = result.Credential.SessionID
|
|
value_24[1] = input.GrantID
|
|
value_24[2] = result.Credential.Challenge
|
|
value_24[3] = result.Credential.ExpiresAt
|
|
values := value_24
|
|
_ = values
|
|
var value_25 string = "INSERT INTO server_authorization_sessions(session_id,grant_id,challenge,expires_at) VALUES(?1,?2,?3,?4)"
|
|
written := StdSqlGo_ExecTx(transaction, context, value_25, values[0:4:4])
|
|
var value_26 bool = written.Error != nil
|
|
var value_27 bool = value_26
|
|
if !value_27 {
|
|
var value_28 Error = StdSqlGo_Commit(transaction)
|
|
value_27 = (value_28 != nil)
|
|
}
|
|
if value_27 {
|
|
AuthorizationHttp_Failure(service, writer, 503, "session unavailable")
|
|
return
|
|
}
|
|
Response_JSON(writer, 200, result)
|
|
}
|
|
|
|
var AuthorizationHttp_ziranInitState uint8
|
|
|
|
func AuthorizationHttp_ziranInit() {
|
|
if AuthorizationHttp_ziranInitState == 2 {
|
|
return
|
|
}
|
|
if AuthorizationHttp_ziranInitState == 1 {
|
|
panic("cyclic module startup")
|
|
}
|
|
AuthorizationHttp_ziranInitState = 1
|
|
Authorization_ziranInit()
|
|
Session_ziranInit()
|
|
TelegramInit_ziranInit()
|
|
TelegramAccountEntry_ziranInit()
|
|
SignedTx_ziranInit()
|
|
TelegramTransport_ziranInit()
|
|
SyncValidation_ziranInit()
|
|
AuthorizationHttp_ziranInitState = 2
|
|
}
|