Skip to content

Commit e56a46d

Browse files
committed
add global npm install ability
1 parent 9123cc6 commit e56a46d

File tree

4 files changed

+117
-4
lines changed

4 files changed

+117
-4
lines changed

README.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,39 @@ This utility works by changing the flow above as follows using a client-server p
3232

3333
This helper is written in Typescript and compiles down to two Javascript scripts, one for the server and one for the client.
3434

35-
### Download
35+
### Option 1: Install via npm (Recommended)
3636

37-
Download the latest release from this repo. The release consists of a filed named `oauth2-forwarder.zip` which contains two Javascript scripts: `o2f-server.js` and `o2f-client.js` plus a helper `browser.sh` script, all in a directory called `o2f`. These can be placed wherever you want, but these instructions assume they are placed in the home directories of the host and container.
37+
You can install oauth2-forwarder globally via npm:
38+
39+
```bash
40+
npm install -g oauth2-forwarder
41+
```
42+
43+
After installation, you'll have the following commands available globally:
44+
- `o2f-server` - Run on the host machine
45+
- `o2f-client` - Run on the container
46+
- `o2f-browser` - Browser script for the container
47+
48+
#### On the host
49+
50+
Run `o2f-server` on the host machine. This will start the server and display the port it's listening on.
51+
52+
#### In the container
53+
54+
1. Set the server info environment variable based on the output from the host:
55+
```bash
56+
export OAUTH2_FORWARDER_SERVER="host.docker.internal:PORT"
57+
```
58+
where PORT is the port displayed when you ran `o2f-server`.
59+
60+
2. Set the BROWSER environment variable to use the browser script:
61+
```bash
62+
export BROWSER=o2f-browser
63+
```
64+
65+
### Option 2: Download Manually
66+
67+
Download the latest release from this repo. The release consists of a file named `oauth2-forwarder.zip` which contains two Javascript scripts: `o2f-server.js` and `o2f-client.js` plus a helper `browser.sh` script, all in a directory called `o2f`. These can be placed wherever you want, but these instructions assume they are placed in the home directories of the host and container.
3868

3969
### On the host
4070

@@ -62,9 +92,25 @@ Notes:
6292

6393
Here's a strategy to make this fairly easy to use with a Docker container built with a Dockerfile.
6494

95+
#### Option 1: Using npm (Recommended)
96+
97+
On the host, set a specific port that you will listen on by configuring the env variable `OAUTH2_FORWARDER_PORT`.
98+
99+
Add these lines in the Dockerfile:
100+
101+
```
102+
RUN npm install -g oauth2-forwarder
103+
ENV OAUTH2_FORWARDER_SERVER host.docker.internal:[PORT]
104+
ENV BROWSER o2f-browser
105+
```
106+
107+
Replace `[PORT]` with the actual port number (or use Docker's `ARG` command).
108+
109+
#### Option 2: Using the zip release
110+
65111
On the host, set a specific port that you will listen on by configuring the env variable `OAUTH2_FORWARDER_PORT`.
66112

67-
Add these lines in the Dockerfile
113+
Add these lines in the Dockerfile:
68114

69115
```
70116
RUN curl -LO https://github.com/sam-mfb/oauth2-forwarder/releases/download/v[VERSION]/oauth2-forwarder.zip

browser-global.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env sh
2+
#
3+
# Assing this script to the BROWSER env variable via:
4+
# export BROWSER=o2f-browser
5+
#
6+
# This will ensure requests to open a browser get forwarded
7+
# through to the proxy
8+
#
9+
LOG_FILE="/tmp/oauth2-forwarder.log"
10+
11+
# fork and return 0 as some apps (e.g., az cli) expect a zero return
12+
# before they will launch their redirect listener
13+
o2f-client "$@" >> "$LOG_FILE" 2>&1 &
14+
15+
exit 0

package.json

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
"version": "1.0.0",
44
"description": "utilities for forwarding oauth2 interactive flow (e.g. container to host)",
55
"main": "dist/index.js",
6+
"bin": {
7+
"o2f-server": "./dist/o2f-server.js",
8+
"o2f-client": "./dist/o2f-client.js",
9+
"o2f-browser": "./browser-global.sh"
10+
},
611
"scripts": {
7-
"build": "rimraf ./dist && webpack",
12+
"build": "rimraf ./dist && webpack && node prepend-shebang.js",
813
"zip-release": "./release.sh",
914
"test": "jest --watch",
1015
"e2e-test": "jest -c ./jest.e2e.config.js",
@@ -14,6 +19,25 @@
1419
},
1520
"author": "Sam Davidoff",
1621
"license": "MIT",
22+
"repository": {
23+
"type": "git",
24+
"url": "https://github.com/sam-mfb/oauth2-forwarder.git"
25+
},
26+
"files": [
27+
"dist",
28+
"browser.sh",
29+
"browser-global.sh"
30+
],
31+
"keywords": [
32+
"oauth2",
33+
"docker",
34+
"container",
35+
"authentication",
36+
"browser"
37+
],
38+
"engines": {
39+
"node": ">=18.0.0"
40+
},
1741
"devDependencies": {
1842
"@eslint/js": "^9.25.1",
1943
"@types/eslint__js": "^8.42.3",

prepend-shebang.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const shebang = '#!/usr/bin/env node\n';
5+
const files = ['o2f-server.js', 'o2f-client.js'];
6+
const distPath = path.join(__dirname, 'dist');
7+
8+
files.forEach(file => {
9+
const filePath = path.join(distPath, file);
10+
11+
if (fs.existsSync(filePath)) {
12+
// Read the file content
13+
const content = fs.readFileSync(filePath, 'utf8');
14+
15+
// Check if shebang is already there
16+
if (!content.startsWith('#!')) {
17+
console.log(`Adding shebang to ${file}`);
18+
// Write the file with shebang prepended
19+
fs.writeFileSync(filePath, shebang + content);
20+
// Make the file executable
21+
fs.chmodSync(filePath, '755');
22+
} else {
23+
console.log(`Shebang already exists in ${file}`);
24+
}
25+
} else {
26+
console.error(`File not found: ${filePath}`);
27+
}
28+
});

0 commit comments

Comments
 (0)