All files / src/internal app-configuration-credential.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 2/2
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 30                3x 3x       1x 1x 1x 1x 1x       1x 1x 1x            
import { createHmacSigner, encodeBytesToBase64, encodeUtf8 } from './storage-encoding'
 
/** Access key authentication of App Configuration: an HMAC over method, path, date, host and body hash. */
export class AppConfigurationKeyCredential {
  private readonly id: string
  private readonly sign: (text: string) => Promise<string>
 
  public constructor(id: string, secret: string) {
    this.id = id
    this.sign = createHmacSigner(secret)
  }
 
  public async signRequest(request: Request): Promise<void> {
    const url = new URL(request.url)
    const date = new Date().toUTCString()
    const body = encodeUtf8(await request.clone().text())
    const contentHash = encodeBytesToBase64(await crypto.subtle.digest('SHA-256', body))
    const signature = await this.sign(
      `${request.method}\n${url.pathname}${url.search}\n${date};${url.host};${contentHash}`,
    )
 
    request.headers.set('x-ms-date', date)
    request.headers.set('x-ms-content-sha256', contentHash)
    request.headers.set(
      'Authorization',
      `HMAC-SHA256 Credential=${this.id}&SignedHeaders=x-ms-date;host;x-ms-content-sha256&Signature=${signature}`,
    )
  }
}