diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index e93fbea..2edc075 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -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": { diff --git a/ex-02/doc/environment_variables.md b/ex-02/doc/environment_variables.md index 505b062..2347b2f 100644 --- a/ex-02/doc/environment_variables.md +++ b/ex-02/doc/environment_variables.md @@ -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
(As documented in the [readme.md](../readme.md)) +* Configure the environment variables for the application
!!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
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-- diff --git a/ex-02/lib/app-config.js b/ex-02/lib/app-config.js index d5e5e03..9f3d009 100644 --- a/ex-02/lib/app-config.js +++ b/ex-02/lib/app-config.js @@ -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; } diff --git a/ex-02/readme.md b/ex-02/readme.md index b93e103..921e563 100644 --- a/ex-02/readme.md +++ b/ex-02/readme.md @@ -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 @@ -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 diff --git a/ex-02/test/app-config.test.js b/ex-02/test/app-config.test.js index cae8fca..cc59289 100644 --- a/ex-02/test/app-config.test.js +++ b/ex-02/test/app-config.test.js @@ -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'); @@ -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(); }); @@ -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(() => { @@ -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"; @@ -166,11 +160,6 @@ test('Set proper PORT value', (t) => { t.end(); }); - - - - - t.end(); }); \ No newline at end of file diff --git a/ex-02/test/app.test.js b/ex-02/test/app.test.js index aa2ced8..735e9e8 100644 --- a/ex-02/test/app.test.js +++ b/ex-02/test/app.test.js @@ -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'); diff --git a/ex-02/test/auth-utils.test.js b/ex-02/test/auth-utils.test.js index f166d75..894338b 100644 --- a/ex-02/test/auth-utils.test.js +++ b/ex-02/test/auth-utils.test.js @@ -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'); diff --git a/src/aa-get-redirect-uri.sh b/src/aa-get-redirect-uri.sh index 627ac85..26c3009 100755 --- a/src/aa-get-redirect-uri.sh +++ b/src/aa-get-redirect-uri.sh @@ -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'/' \ No newline at end of file +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'/' \ No newline at end of file