# re-az-authentication

> Authenticate with the Azure REST API.

Latest version **0.3.0** (published 2020-01-28) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install re-az-authentication
pnpm add re-az-authentication
yarn add re-az-authentication
bun add re-az-authentication
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2020-01-28 |
| First published | 2019-02-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.5.0 |
| Dependencies | 1 |
| Unpacked size | 20.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Karl Wallenius |
| Maintainers | karlgw |
| Keywords | Authentication, Azure, Resouce Manager, Azure Resource Manager |

## Links

- npm: https://www.npmjs.com/package/re-az-authentication
- Repository: https://github.com/RedeployAB/re-az-authentication
- Homepage: https://github.com/RedeployAB/re-az-authentication#readme
- Issues: https://github.com/RedeployAB/re-az-authentication/issues
- npm.io page: https://npm.io/package/re-az-authentication

## Dependencies (1)

- [webreq](https://npm.io/package/webreq.md) ^0.6.0

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 0.3.0 (latest) — 2020-01-28
- 0.2.2 — 2020-01-20
- 0.2.1 — 2019-08-05
- 0.2.0 — 2019-06-14
- 0.1.1 — 2019-02-21
- 0.1.0 — 2019-02-21

## README

# re-az-authentication

Authenticate with the Azure REST API.

**Content**

* [Information](#information)
* [Install](#install)
* [Usage](#usage)

## Information

Small and lightweight module to handle authentication against the Azure REST API.

*Supported authentication styles (v0.1.0)*:

* Service Principals (Azure AD Applications)
* MSI (Managed Identity)

## Install

```
npm install re-az-authentication
```

## Usage

**Service Principals**

**With environment variables AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_TENANT_ID set*
```js
const AZAuthentication = require('re-az-authentication');

// Promise chaining.
AZAuthentication.authenticateWithServicePrincipal()
  .then(credentials => {
    let headers: { Authorization: `Bearer ${credentials.access_token}`}
    return webreq.get('https://management.azure.com/subscriptions?api-version=2016-06-01', { headers: headers });
  })
  .then(subscriptions => {
      // And so on.
  })
  .catch(error => {
      console.log(error);
  });

// Async/Await.
let credentials, subscriptions;
try {
  credentials = await AZAuthentication.authenticateWithServicePrincipal();
  let headers: { Authorization: `Bearer ${credentials.access_token}`}
  subscriptions = await webreq.get('https://management.azure.com/subscriptions?api-version=2016-06-01', { headers: headers });
} catch (error) {
  console.log(error);
}
```

*To specify a specific resource to authenticate with, specify it in `options`*
```js
const AZAuthentication = require('re-az-authentication');

let credentials, secrets;
try {
  credentials = await AZAuthentication.authenticateWithServicePrincipal({ resource: 'https://vault.azure.net' });
  let headers: { Authorization: `Bearer ${credentials.access_token}`}
  secrets = await webreq.get('https://<vault>.vault.azure.net/secrets?api-version=7.0', { headers: headers });
} catch (error) {
  console.log(error);
}

// Or with a predefined type.
let credentials, secrets;
try {
  credentials = await AZAuthentication.authenticateWithServicePrincipal({ type: 'keyvault' });
  let headers: { Authorization: `Bearer ${credentials.access_token}`}
  secrets = await webreq.get('https://<vault>.vault.azure.net/secrets?api-version=7.0', { headers: headers });
} catch (error) {
  console.log(error);
}
```

*To specify Azure Cloud to authenticate with, specify it in `options`*
```js
// Supported Azure Clouds:
//
// 'azure' (default)
// 'azureUSGovernment'
// 'azureGermany'
// 'azureChina'
//
// or:
//
// AZAuthentication.AZURE (default)
// AZAuthentication.AZURE_US_GOVERNMENT
// AZAuthentication.AZURE_GERMANY
// AZAuthentication.AZURE_CHINA

const AZAuthentication = require('re-az-authentication');

let credentials;

try {
  credentials = await AZAuthentication.authenticateWithServicePrincipal({ environment: 'azureGermany' });
  let headers: { Authorization: `Bearer ${credentials.access_token}`}
  subscriptions = await webreq.get('https://management.azure.com/subscriptions?api-version=2016-06-01', { headers: headers });
} catch (errpr) {
  console.log(error)
}

// Alternative.
try {
  credentials = await AZAuthentication.authenticateWithServicePrincipal({ environment: AZAuthentication.AZURE_GERMANY });
  let headers: { Authorization: `Bearer ${credentials.access_token}`}
  subscriptions = await webreq.get('https://management.azure.com/subscriptions?api-version=2016-06-01', { headers: headers });
} catch (errpr) {
  console.log(error)
}
```

**Managed Identity (MSI)**

This requries that the Azure Resource has **Identity** enabled.
This sets the environment variables `MSI_ENDPOINT` and `MSI_SECRET`.

The default behaviour is using system assigned identity.

To use user assigned identity either set `MSI_CLIENT_ID` or pass
`clientId: <msi-client-id>` in the options.

*System assigned*
```js
const AZAuthentication = require('re-az-authentication');

// Promise chaining.
AZAuthentication.authenticateWithMSI({type: 'keyvault'})
  .then(credentials => {
    let headers: { Authorization: `Bearer ${credentials.access_token}`}
    return webreq.get('https://<vault>.vault.azure.net/secrets?api-version=7.0', { headers: headers });
  })
  .then(secrets => {
      // And so on.
  })
  .catch(error => {
      console.log(error);
  });

// Async/Await.
let credentials, secrets;
try {
  credentials = await AZAuthentication.authenticateWithMSI({ resource: 'https://vault.azure.net' });
  let headers: { Authorization: `Bearer ${credentials.access_token}`}
  secrets = await webreq.get('https://<vault>.vault.azure.net/secrets?api-version=7.0', { headers: headers });
} catch (error) {
  console.log(error);
}
```

*User assigned*
```js
const AZAuthentication = require('re-az-authentication');

// Promise chaining.
// Optionally instead of passing clientId, set an environment variable,
// MSI_CLIENT_ID.
AZAuthentication.authenticateWithMSI({type: 'keyvault', clientId: '<ID>'})
  .then(credentials => {
    let headers: { Authorization: `Bearer ${credentials.access_token}`}
    return webreq.get('https://<vault>.vault.azure.net/secrets?api-version=7.0', { headers: headers });
  })
  .then(secrets => {
      // And so on.
  })
  .catch(error => {
      console.log(error);
  });

// Async/Await.
// Optionally instead of passing clientId, set an environment variable,
// MSI_CLIENT_ID.
let credentials, secrets;
try {
  credentials = await AZAuthentication.authenticateWithMSI({ resource: 'https://vault.azure.net', clientId: '<ID>' });
  let headers: { Authorization: `Bearer ${credentials.access_token}`}
  secrets = await webreq.get('https://<vault>.vault.azure.net/secrets?api-version=7.0', { headers: headers });
} catch (error) {
  console.log(error);
}
```

*To specify Azure Cloud to authenticate with, specify it in `options`*
```js
// Supported Azure Clouds:
//
// 'azure' (default)
// 'azureUSGovernment'
// 'azureGermany'
// 'azureChina'
//
// or:
//
// AZAuthentication.AZURE (default)
// AZAuthentication.AZURE_US_GOVERNMENT
// AZAuthentication.AZURE_GERMANY
// AZAuthentication.AZURE_CHINA
const AZAuthentication = require('re-az-authentication');

let credentials, secrets;
try {
  credentials = await AZAuthentication.authenticateWithMSI({ environment: 'azureGermany' });
  let headers: { Authorization: `Bearer ${credentials.access_token}`}
  subscriptions = await webreq.get('https://management.azure.com/subscriptions?api-version=2016-06-01', { headers: headers });
} catch (error) {
  console.log(error);
}

// Alternative.
try {
  credentials = await AZAuthentication.authenticateWithMSI({ environment: AZAuthentication.AZURE_GERMANY });
  let headers: { Authorization: `Bearer ${credentials.access_token}`}
  subscriptions = await webreq.get('https://management.azure.com/subscriptions?api-version=2016-06-01', { headers: headers });
} catch (error) {
  console.log(error);
}
```

---
_Source: https://npm.io/package/re-az-authentication · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
