Code Stream - Custom Integration - Generate Basic Authentication Header

Written by Sam McGeown
Published on 4/11/2020 - Read in about 3 min (429 words)

To generate a basic authentication header from a username and password in Code Stream you could use a CI task and execute echo -n username:password | base64 in the shell then export the result for use later on. A more repeatable way is to create a Custom Integration that takes the two inputs, and returns the encoded header as an output.

To create the Custom Integration:

  1. Create a new Custom Integration named “Create Basic Authentication Header”
  2. Select the Runtime - the examples below are shell and python3 respectively
  3. Replace the placeholder code with the example from below
  4. Save and version the Custom Integration, ensuring you eanble the “Release Version” toggle
Creating the Custom Integration
Creating the Custom Integration

To use the Custom Integration in a pipeline:

  1. Configure the Host (you’ll need a Docker endpoint configured for this) and Builder image URL settings in the Workspace tab of your pipeline (e.g. python:latest)
  2. Add a new Custom task, to your Stage, select the “Create Basic Authentication Header” integration and configure the inputs
  3. Use the tasks output.properties to refer to the header value in your later tasks - e.g. I’m using ${Stage0.Create Auth Header.output.properties.basicAuthHeader} to refer to the output in a REST task below:
Consuming the Custom Integration
Consuming the Custom Integration

Below are the shell and a Python3 examples, in the YAML format to paste into your custom integration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
---
runtime: shell
code: |
    export basicAuthHeader=$(echo -n $username:$password | base64)
inputProperties:      # Enter fields for input section of a task
  # Username input
  - name: 'username'
    type: text
    title: 'Username'
    placeHolder: 'Enter basic authentication usename'
    required: true
    bindable: true
  # Password input
  - name: 'password'
    type: password
    title: 'Password'
    placeHolder: 'Enter basic authentication password'
    defaultValue: ''
    required: true
    bindable: true
outputProperties:
  - name: basicAuthHeader
    type: label
    title: Basic Authentication Header
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
---
runtime: "python3"
code: |
  from base64 import b64encode
  from context import getInput, setOutput
  username = getInput('username')
  password = getInput('password')
  usernameAndPassword = b64encode(bytes(f'{username}:{password}',encoding='ascii')).decode('ascii')
  setOutput('basicAuthHeader', "Basic "+usernameAndPassword)  
inputProperties:      # Enter fields for input section of a task
  # Username input
  - name: 'username'
    type: text
    title: 'Username'
    placeHolder: 'Enter basic authentication usename'
    required: true
    bindable: true
  # Password input
  - name: 'password'
    type: password
    title: 'Password'
    placeHolder: 'Enter basic authentication password'
    defaultValue: ''
    required: true
    bindable: true
outputProperties:
  - name: basicAuthHeader
    type: label
    title: Basic Authentication Header
Share this post