This repo contains the Layer8 Resource/Authentication Server, Proxy, and Service Provider Mocks.
In conjunction with the Layer8_Interceptor & the Layer8_Middleware (available throught npm), this repo forms the proof of concept for the Layer8 E2E encryption proxy to be made available through a WASM module in the browser.
The directories in the folder sp_mocks represent test cases and experimental projects to test various aspects of the system. These can be used as reference for development.
Note: the tunnel is created everytime the page is reloaded. Therefore, Layer8 works best with single page applications.
Currently the proof of concept only works with node.js in the backend and is tightly coupled to the express.js package.
1) Download and install Golang
2) Navigate to your project frontend directory (or the sp_mock you want to run). Run npm install layer8_interceptor
(v0.0.17 at the time of writting).
3) Naviate to your project backend directory (or the sp_mock you want to run). Run npm install layer8_middleware
(v0.0.17 at the time of writting).
4) From the layer8 home directory, run:
$cd ./server && go mod tidy
cd server && go run main.go -port=5001 -jwtKey=secret -MpKey=secret -UpKey=secret -ProxyURL=http://localhost:5001
5) Clone .env.dev
to .env
in the frontend
and backend
directories of the sp_mock you are using.
6) Run your frontend / backend. If using that provided:cd sp_mocks/wgp/frontend && npm i && npm run dev
cd sp_mocks/wgp/backend && npm i && npm run dev
cd sp_mocks/imsharer/frontend && npm run dev
cd sp_mocks/imsharer/backend && npm run dev
After installing the NPM package, import the library and initialize an encrypted tunnel as follows (usually in the App.vue file):
// Top level imports
import layer8_interceptor from 'layer8_interceptor'
[...]
try{
layer8_interceptor.initEncryptedTunnel({
providers: [BACKEND_URLs], // An array of service providers you want to connect to
proxy: PROXY_URL, // Your local proxy instance. Necessary only if the mode is "dev".
}, "dev") // If omitted, the default is production mode.
}catch(err){
console.log(".initEncryptedTunnel error: ", err)
}
Once the encrypted tunnel is open, you can use the layer8_interceptor.fetch()
method analogous to the native browser fetch()
. Just like the native fetch()
, a promise is returned and should be properly awaited within a asynchronous function.
// Top level imports
import layer8_interceptor from 'layer8_interceptor'
[...]
// Within asynchronous functions
let getResponse = await layer8_interceptor.fetch( URL )
let postResponse = await layer8_interceptor.fetch( URL, {
method: "POST",
headers: {
"Content-Type": "Application/Json",
},
body: JSON.stringify({
key: value,
}),
});
// Top level imports
import layer8_interceptor from 'layer8_interceptor'
[...]
// Within asynchronous functions
const url = await layer8.static(image.url);
[...]
// Within the HTML
<img :src="https://github.com/globe-and-citizen/layer8/raw/main/url" />
Use the Layer8 middleware as any other node package. At present, no configuration of the encrypted tunnel is necessary. Note: Currently Layer8 is being built tightly coupled with ExpresJS.
// Top Level Imports
const layer8 = require('layer8_middleware');
// Node Initialization
app.use(layer8.tunnel); // for the encrypted tunnel
app.use('/media', layer8.static('uploads')); // in order to connect a public folder
// Usage
app.get('/', (req, res) => {
res.json({ key: val })
});
app.post("/route", async (req, res) => {
const body = req.body;
try {
console.log(body)
res.status(200).send("User registered successfully!");
} catch (err) {
console.log("err: ", err);
res.status(500).send({ error: "Something went wrong!" });
}
});
Note: During routine usage, there are no special calls necessary to make use of Layer8. The res.json() & res.send() & res.end() have been overwritten by Layer8 and will be used automatically.
1) Using express.json() as middleware in main file is unnecessary. The layer8_middleware automatically parses the incoming request. If you include the line app.use(express.json()) requests will get "caught" in the express.json() middleware and not reach your other endpoints.
docker compose -f docker-compose-influx.yml up
INFLUXDB_URL
as http://host.docker.internal:8086
and INFLUXDB_TOKEN
value as based on the created token variable to .env
file to run the telegraf container.docker compose -f docker-compose-telegraf.yml up
OTEL_EXPORTER_OTLP_ENDPOINT
to localhost:4317 then add it to the layer8 server environment variable file.First run make go_mod_tidy
and then start layer8 locally using make run_server_local
Do make npm_install_imsharer
to install Imsharer dependencies
Do make run_imsharer_frontend
to start Imsharer frontend
Do make run_imsharer_backend
to start Imsharer backend
Navigate to http://localhost:5173
to see the Imsharer frontend
To fix the tunnel and register your backend with layer8, navigate to http://localhost:5001/client-register-page
and register as a client with your backend URL as localhost:6001
Navigate back to http://localhost:5173
and you should see the tunnel working