This commit is contained in:
hatim 2023-05-18 19:50:39 +02:00
parent e3c32a5650
commit dc920d5e0c
8 changed files with 2273 additions and 2187 deletions

File diff suppressed because it is too large Load diff

View file

@ -85,7 +85,7 @@ export default (methods: Types.ServerMethods, opts: ServerOptions) => {
if (!methods.AddApp) throw new Error('method: AddApp is not implemented') if (!methods.AddApp) throw new Error('method: AddApp is not implemented')
const authContext = await opts.AdminAuthGuard(req.headers['authorization']) const authContext = await opts.AdminAuthGuard(req.headers['authorization'])
const request = req.body const request = req.body
const error = Types.AddAppRequestValidate(request) const error = Types.AuthAppRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger) if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query const query = req.query
const params = req.params const params = req.params
@ -93,6 +93,20 @@ export default (methods: Types.ServerMethods, opts: ServerOptions) => {
res.json({status: 'OK', ...response}) res.json({status: 'OK', ...response})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e } } catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
}) })
if (!opts.allowNotImplementedMethods && !methods.AuthApp) throw new Error('method: AuthApp is not implemented')
app.post('/api/admin/app/auth', async (req, res) => {
try {
if (!methods.AuthApp) throw new Error('method: AuthApp is not implemented')
const authContext = await opts.AdminAuthGuard(req.headers['authorization'])
const request = req.body
const error = Types.AuthAppRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.AuthApp({ ...authContext, ...query, ...params }, request)
res.json({status: 'OK', ...response})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.GetApp) throw new Error('method: GetApp is not implemented') if (!opts.allowNotImplementedMethods && !methods.GetApp) throw new Error('method: GetApp is not implemented')
app.post('/api/app/get', async (req, res) => { app.post('/api/app/get', async (req, res) => {
try { try {

View file

@ -62,7 +62,7 @@ export default (params: ClientParams) => ({
} }
return { status: 'ERROR', reason: 'invalid response' } return { status: 'ERROR', reason: 'invalid response' }
}, },
AddApp: async (request: Types.AddAppRequest): Promise<ResultError | ({ status: 'OK' }& Types.AddAppResponse)> => { AddApp: async (request: Types.AuthAppRequest): Promise<ResultError | ({ status: 'OK' }& Types.AuthApp)> => {
const auth = await params.retrieveAdminAuth() const auth = await params.retrieveAdminAuth()
if (auth === null) throw new Error('retrieveAdminAuth() returned null') if (auth === null) throw new Error('retrieveAdminAuth() returned null')
let finalRoute = '/api/admin/app/add' let finalRoute = '/api/admin/app/add'
@ -71,7 +71,21 @@ export default (params: ClientParams) => ({
if (data.status === 'OK') { if (data.status === 'OK') {
const result = data const result = data
if(!params.checkResult) return { status: 'OK', ...result } if(!params.checkResult) return { status: 'OK', ...result }
const error = Types.AddAppResponseValidate(result) const error = Types.AuthAppValidate(result)
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
}
return { status: 'ERROR', reason: 'invalid response' }
},
AuthApp: async (request: Types.AuthAppRequest): Promise<ResultError | ({ status: 'OK' }& Types.AuthApp)> => {
const auth = await params.retrieveAdminAuth()
if (auth === null) throw new Error('retrieveAdminAuth() returned null')
let finalRoute = '/api/admin/app/auth'
const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
const result = data
if(!params.checkResult) return { status: 'OK', ...result }
const error = Types.AuthAppValidate(result)
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message } if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
} }
return { status: 'ERROR', reason: 'invalid response' } return { status: 'ERROR', reason: 'invalid response' }

File diff suppressed because it is too large Load diff

View file

@ -96,12 +96,18 @@ service LightningPub {
// <App> // <App>
rpc AddApp(structs.AddAppRequest) returns (structs.AddAppResponse) { rpc AddApp(structs.AuthAppRequest) returns (structs.AuthApp) {
option (auth_type) = "Admin"; option (auth_type) = "Admin";
option (http_method) = "post"; option (http_method) = "post";
option (http_route) = "/api/admin/app/add"; option (http_route) = "/api/admin/app/add";
}; };
rpc AuthApp(structs.AuthAppRequest) returns (structs.AuthApp) {
option (auth_type) = "Admin";
option (http_method) = "post";
option (http_route) = "/api/admin/app/auth";
}
rpc GetApp(structs.Empty) returns (structs.Application) { rpc GetApp(structs.Empty) returns (structs.Application) {
option (auth_type) = "App"; option (auth_type) = "App";
option (http_method) = "post"; option (http_method) = "post";

View file

@ -25,7 +25,7 @@ message LndGetInfoResponse {
string alias = 1; string alias = 1;
} }
message AddAppRequest { message AuthAppRequest {
string name = 1; string name = 1;
} }
@ -34,7 +34,7 @@ message Application {
string id = 2; string id = 2;
int64 balance = 3; int64 balance = 3;
} }
message AddAppResponse { message AuthApp {
Application app = 1; Application app = 1;
string auth_token = 2; string auth_token = 2;
} }

View file

@ -45,13 +45,25 @@ export default class {
} }
async AddApp(req: Types.AddAppRequest): Promise<Types.AddAppResponse> { async AddApp(req: Types.AuthAppRequest): Promise<Types.AuthApp> {
const app = await this.storage.applicationStorage.AddApplication(req.name) const app = await this.storage.applicationStorage.AddApplication(req.name)
return { return {
app: { app: {
id: app.app_id, id: app.app_id,
name: app.name, name: app.name,
balance: 0 balance: app.owner.balance_sats
},
auth_token: this.SignAppToken(app.app_id)
}
}
async AuthApp(req: Types.AuthAppRequest): Promise<Types.AuthApp> {
const app = await this.storage.applicationStorage.GetApplication(req.name)
return {
app: {
id: app.app_id,
name: app.name,
balance: app.owner.balance_sats
}, },
auth_token: this.SignAppToken(app.app_id) auth_token: this.SignAppToken(app.app_id)
} }

View file

@ -110,12 +110,19 @@ export default (mainHandler: Main): Types.ServerMethods => {
throw new Error("unimplemented") throw new Error("unimplemented")
}, },
AddApp: async (ctx, req) => { AddApp: async (ctx, req) => {
const err = Types.AddAppRequestValidate(req, { const err = Types.AuthAppRequestValidate(req, {
name_CustomCheck: name => name !== '' name_CustomCheck: name => name !== ''
}) })
if (err != null) throw new Error(err.message) if (err != null) throw new Error(err.message)
return mainHandler.applicationManager.AddApp(req) return mainHandler.applicationManager.AddApp(req)
}, },
AuthApp: async (ctx, req) => {
const err = Types.AuthAppRequestValidate(req, {
name_CustomCheck: name => name !== ''
})
if (err != null) throw new Error(err.message)
return mainHandler.applicationManager.AuthApp(req)
},
GetApp: async (ctx) => { GetApp: async (ctx) => {
return mainHandler.applicationManager.GetApp(ctx.app_id) return mainHandler.applicationManager.GetApp(ctx.app_id)
}, },