Skip to content

Commit

Permalink
Update ex-01 p2
Browse files Browse the repository at this point in the history
  • Loading branch information
larskaare authored Nov 17, 2023
1 parent f9f844e commit 8c2b576
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"postCreateCommand": {
"sed": "sed -i 's/ZSH_THEME=\\\"devcontainers\\\"/ZSH_THEME=\\\"avit\\\"/g' ~/.zshrc",
"addPath": "echo 'export PATH=\\$PATH:$CODESPACE_VSCODE_FOLDER/src' >> ~/.zshrc",
"addPath": "echo 'export PATH=$PATH:$CODESPACE_VSCODE_FOLDER/src' >> ~/.zshrc",
"welcomeMessage": "sudo cp .devcontainer/welcome.txt /usr/local/etc/vscode-dev-containers/first-run-notice.txt"
},
"containerEnv": {
Expand Down
21 changes: 12 additions & 9 deletions ex-02/doc/environment_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ In this part we will start to investigate how we move dynamic configuration para

Steps:

* Configure the environment variables for the application<br/>(As documented in the [readme.md](../readme.md))
* Configure the environment variables for the application</br>!!Notice the whitespace in front of the second line? This prevents the command from entering shell command history

```shell
export NODE_ENV=production
export CLIENT_SECRET='the client secret from the AD app object'
export CLIENT_ID="the client id from the AD app object"
export TENANT_ID="the tenant id"
export PORT=3000
export REDIRECT_ID=$(aa-get-redirect-uri.sh)
```
* The `aa-get-redirect-uri.sh` scripts helps to extract and generate a redirect uri for your workspace</br>Examine the script at `../src/aa-get-redirect-uri.sh` (It's automatically added to the path)
* If you have create a new code space since you configured the Entra ID Application Object for the client app, you may need to update the app registration with the proper redirect uri.

```shell

export NODE_ENV=production
export CLIENT_SECRET='the client secret from the AD app object'
export CLIENT_ID="the client id from the AD app object"
export TENANT_ID="the tenant id"
export PORT=3000
```

## --Now You--

Expand Down
14 changes: 10 additions & 4 deletions ex-02/lib/app-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,32 @@ const serverConfig = {
const clientConfig = {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
redirect_uri: ['http://localhost:' + port +'/callback'],
redirect_uri: process.env.REDIRECT_URI
};

function isConfigOk() {

if (__.isUndefined(tenantId)) {
logger.error('Config: Missing Tenant_Id in config');
logger.error('Config: Missing tenant_id in config');
return false;
}

if (__.isUndefined(clientConfig.client_id)) {
logger.error('Config: Missing Client_Id in config');
logger.error('Config: Missing client_Id in config');
return false;
}

if (__.isUndefined(clientConfig.client_secret)) {
logger.error('Config: Missing Client_Secret in config');
logger.error('Config: Missing client_Secret in config');
return false;
}

if (__.isUndefined(clientConfig.redirect_uri)) {
logger.error('Config: Missing redirect_uri in config');
return false;
}


return true;
}

Expand Down
3 changes: 2 additions & 1 deletion ex-02/readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# EX-2 - Getting an access token using code

The purpose of this exercise is get an access token using code. We are moving from the raw style using bare http requests to start exploring what's needed to get this done in code.
The purpose of this exercise is get an access token using code. We are moving from the raw style http to exploring what's needed to get this done in code.

## Outline

Expand Down Expand Up @@ -54,6 +54,7 @@ Expects the following environment variables to execute properly
export CLIENT_ID=""
export TENANT_ID=""
export PORT=3000
export REDIRECT_URI=https://...../callback

## Run

Expand Down
61 changes: 25 additions & 36 deletions ex-02/test/app-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test('Environment Config should be persisted', (t) => {
process.env.TENANT_ID = 'A';
process.env.CLIENT_ID = 'B';
process.env.CLIENT_SECRET = 'C';
process.env.REDIRECT_URI = 'D';

delete require.cache[require.resolve('../lib/app-config.js')];
const appConfig = require('../lib/app-config.js');
Expand All @@ -20,6 +21,8 @@ test('Environment Config should be persisted', (t) => {
'C',
'Client Secret should be set'
);
t.equal(appConfig.clientConfig.redirect_uri, 'D', 'Redirect URI should be set')


t.end();
});
Expand All @@ -31,6 +34,7 @@ test('IsConfigOk', (t) => {
process.env.TENANT_ID = 'A';
process.env.CLIENT_ID = 'B';
process.env.CLIENT_SECRET = 'C';
process.env.REDIRECT_URI = 'D';

sinon.stub(process, 'exit');
process.exit.callsFake(() => {
Expand Down Expand Up @@ -108,54 +112,44 @@ test('IsConfigOk', (t) => {
t.end();
});

t.test('Verify that port is used in client redirect uri', (t) => {
delete process.env.PORT;
process.env.PORT = 3333;

delete require.cache[require.resolve('../lib/app-config.js')];
const appConfig = require('../lib/app-config.js');

t.equal(appConfig.clientConfig.redirect_uri[0],'http://localhost:3333/callback','Redirect uri should include port');
t.equal(appConfig.port,"3333",'Port returned from config should be ' + process.env.PORT);

t.end();
});

t.end();
});

test('Set proper PORT value', (t) => {

test('Set proper PORT value', async (t) => {


delete require.cache[require.resolve('../lib/app-config.js')];

delete process.env.PORT;

const port = require('../lib/app-config.js').port;

t.equal(port, "3000", 'No env PORT default to value 3000');

t.end();
});
t.beforeEach(function () {
//Defining config
process.env.TENANT_ID = 'A';
process.env.CLIENT_ID = 'B';
process.env.CLIENT_SECRET = 'C';
process.env.REDIRECT_URI = 'D';

sinon.stub(process, 'exit');
process.exit.callsFake(() => {
console.log('Test triggered process.exit');
return true;
});
});

test('Set proper PORT value', (t) => {
t.afterEach(function() {
process.exit.restore();
});

t.test('Env PORT does not exist', (t) => {

t.test('Env PORT does not exist', async (t) => {
delete require.cache[require.resolve('../lib/app-config.js')];

delete process.env.PORT;

const port = require('../lib/app-config.js').port;

t.equal(port, '3000', 'No env PORT default to value 3000');

t.end();
});

t.test('Env PORT is set', (t) => {
t.test('Env PORT is set', async (t) => {

delete require.cache[require.resolve('../lib/app-config.js')];

process.env.PORT = "5999";
Expand All @@ -166,11 +160,6 @@ test('Set proper PORT value', (t) => {

t.end();
});






t.end();
});
1 change: 1 addition & 0 deletions ex-02/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
process.env.TENANT_ID = 'A';
process.env.CLIENT_ID = 'B';
process.env.CLIENT_SECRET = 'C';
process.env.REDIRECT_URI = 'D';

const { test } = require('tap');
const app = require('../src/app');
Expand Down
1 change: 1 addition & 0 deletions ex-02/test/auth-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
process.env.TENANT_ID = 'A';
process.env.CLIENT_ID = 'B';
process.env.CLIENT_SECRET = 'C';
process.env.REDIRECT_URI = 'D';

const { test } = require('tap');
const authUtils = require('../lib/auth-utils.js');
Expand Down
20 changes: 19 additions & 1 deletion src/aa-get-redirect-uri.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,22 @@ source "$CONFIG_FILE" 2> /dev/null

# printf "Successfully read config file (%s)\n" "$CONFIG_FILE"

echo 'https://'$CODESPACE_NAME'-3000.'$GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN'/'
if [ -z "$PORT" ]
then
printf "Missing PORT environment variable.\n"
exit 1
fi

if [ -z "$CODESPACE_NAME" ]
then
printf "Missing CODESPACE_NAME environment variable.\n"
exit 1
fi

if [ -z "$GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN" ]
then
printf "Missing $GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN environment variable.\n"
exit 1
fi

echo 'https://'$CODESPACE_NAME'-'$PORT'.'$GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN'/'

0 comments on commit 8c2b576

Please sign in to comment.