# TokenUtils

## TokenUtils{#TokenUtils}

Module with helper functions for the Tokens.

### extractServiceTokenFromRequest(request) {#extractServiceTokenFromRequest}⇒ `Object`

Extracts and decodes a service token from a raw request

**Returns**: `Object` - serviceToken - The service token

| Param   | Type                   |
| ------- | ---------------------- |
| request | `http.IncomingMessage` |

### extractImidTokenFromRequest(request) {#extractImidTokenFromRequest}⇒ `String` | `null`

Extracts IMID token from a raw request if present

**Returns**: `String` | `null` - imidToken - The IMID token if present

| Param   | Type                   |
| ------- | ---------------------- |
| request | `http.IncomingMessage` |

### getSubject(request) {#getSubject}⇒ `String`

Get the subject from the service token

**Returns**: `String` - organization - The subject identifier set on the service token

| Param   | Type                   |
| ------- | ---------------------- |
| request | `http.IncomingMessage` |

### getOrganization(request) {#getOrganization}⇒ `String`

Get the subject's organization

**Returns**: `String` - organization - The organization the subject belongs to

| Param   | Type                   |
| ------- | ---------------------- |
| request | `http.IncomingMessage` |

### getUnits(request) {#getUnits}⇒ `Array.<String>`

Get the subject's mapped units

**Returns**: `Array.<String>` - units - An array of all units the subject belongs to

| Param   | Type                   |
| ------- | ---------------------- |
| request | `http.IncomingMessage` |

### getSelectedUnit(request) {#getSelectedUnit}⇒ `null` | `String`

Get the subject's selected unit

**Returns**: `null` | `String` - unit - The subject's selected unit, null if no unit selected

| Param   | Type                   |
| ------- | ---------------------- |
| request | `http.IncomingMessage` |

### getOrgPermissions(request) {#getOrgPermissions}⇒ `Array.<String>`

Get the subject's organization permissions

Organization permissions are located under permissions.org

**Returns**: `Array.<String>` - } permissions - The subject's org permissions

| Param   | Type                   |
| ------- | ---------------------- |
| request | `http.IncomingMessage` |

### getUnitPermissions(request, unit) {#getUnitPermissions}⇒ `Array.<String>`

Get the subject's permissions for the specified unit

Unit permissions are located under permissions.units\[unit]

**Returns**: `Array.<String>` - permissions - The subject's permissions for the specified unit

| Param   | Type                   | Description                                              |
| ------- | ---------------------- | -------------------------------------------------------- |
| request | `http.IncomingMessage` | **Required -**                                           |
| unit    | `String`               | **Required -** The unit permissions should be checked in |

### isServiceAdmin(request) {#isServiceAdmin}⇒ `Boolean`

Checks if a token belogs to an admin for the service

**Returns**: `Boolean` - isServiceAdmin - True if the token belongs to an admin for the service

| Param   | Type                   |
| ------- | ---------------------- |
| request | `http.IncomingMessage` |

### getUserinfo(request) {#getUserinfo}⇒ `Object`

Get the subject's userinfo

**Returns**: `Object` - userinfo - The userinfo object set on the subject

| Param   | Type                   |
| ------- | ---------------------- |
| request | `http.IncomingMessage` |
