# Automating preauthorization using the API

**URL:** https://hub.mender.io/t/automating-preauthorization-using-the-api/3152
**Category:** General Discussions
**Tags:** raspberry-pi-4, preauthorization
**Created:** [January 28, 2021, 10:38pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152 "2021-01-28T22:38:00Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)
#### Post date: [January 28, 2021, 10:38pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/1 "2021-01-28T22:38:00Z")

</div>

I’ve set up a cloud function in Firebase Functions that my device triggers by publishing to a pubsub topic. The cloud function makes the `fetch` request to the preAuth endpoint;

`https://hosted.mender.io/api/management/v2/devauth/devices'`.

The plan being that my device manufacturer would boot the device to test it before shipping to the customer. This sets the device up to receive software updates as soon as the customer receives it and boots it again for their use.

In my logs I get the response from the `POST` request in the promise callback as shown in the API example;

`body: {"domain":{"domain":null,"_events":{},"_eventsCount":3,"members":[]}}`

The problem is I don’t see the device show up in any of the hosted Mender categories(`Pending`, `Preauthorized` or `rejected`).

if anyone can help with what I may be missing it would be appreciated.

Thanks

---

<div class="post-metadata">

### Author: ![drewmoseley](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/drewmoseley/32/47_2.png) [@drewmoseley](https://hub.mender.io/u/drewmoseley)
#### Post date: [January 29, 2021, 7:11pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/2 "2021-01-29T19:11:28Z")

</div>

Hi @bradw, for preauthorization, you need to do a POST to the API endpoint with the identity data and public key of the device. See this link for full details.  
[https://docs.mender.io/api/#preauthorize](https://docs.mender.io/api/#preauthorize)  
Drew

---

<div class="post-metadata">

### Author: ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)
#### Post date: [January 29, 2021, 8:11pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/3 "2021-01-29T20:11:00Z")

</div>

Hi @drewmoseley,

That’s what I did from my Firebase Functions backend. Here is the code. I got the response I posted above back but nothing showed up under my hosted Mender dashboard.

```
const fetch = require('node-fetch');
const functions = require('firebase-functions');

module.exports = functions.pubsub.topic('menderPreauthorize').onPublish(async (message) => {
    const deviceId = message.data ? Buffer.from(message.data, 'base64').toString() : null;
    let res
    if (deviceId) {
      const sn = deviceId.split('y')[1]
      res = await fetch ('https://hosted.mender.io/api/management/v2/devauth/devices',
        {
           method: 'POST',
           body: {
             "identity_data": {
                "sku": deviceId,
                "sn": sn
              },
              pubkey: process.env.MENDER_PUBKEY
            },
            headers: {
              'Content-Type': 'application/json',
              'Accept': 'application/json',
              'Authorization': process.env.MENDER_API_KEY
          }
       })
       .then((res) => {
          console.log(`body: ${JSON.stringify(res.json())}`)
          return res.json
        })
        .catch((err) => {
           console.log(`Error: ${JSON.stringify(err)}`)
           return err
        })
    } else {
      res = 'deviceId was null.'
    }
    return res
 })
```

---

<div class="post-metadata">

### Author: ![drewmoseley](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/drewmoseley/32/47_2.png) [@drewmoseley](https://hub.mender.io/u/drewmoseley)
#### Post date: [January 29, 2021, 10:38pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/4 "2021-01-29T22:38:10Z")

</div>

@merlin can you help here?

---

<div class="post-metadata">

### Author: ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)
#### Post date: [February 1, 2021, 6:11pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/5 "2021-02-01T18:11:02Z")

</div>

I’m still hung up on this. It’s one of the last things I wanted to have handled before sending the image over to my prototype manufacturer.

---

<div class="post-metadata">

### Author: ![drewmoseley](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/drewmoseley/32/47_2.png) [@drewmoseley](https://hub.mender.io/u/drewmoseley)
#### Post date: [February 1, 2021, 6:36pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/6 "2021-02-01T18:36:12Z")

</div>

I don’t see anything obviously wrong with this but it’s hard to know if the env settings are formatted correctly or if the other data types are properly converted to strings before providing them to the API. Is there any debug you can enable to see the exact contents of the API call?  
Drew

---

<div class="post-metadata">

### Author: ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)
#### Post date: [February 1, 2021, 6:55pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/7 "2021-02-01T18:55:55Z")

</div>

Environment variables get plugged in through Google Cloud’s dashboard and there is an output log for errors, success, `console.log`, etc.

---

<div class="post-metadata">

### Author: ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)
#### Post date: [February 1, 2021, 7:35pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/8 "2021-02-01T19:35:47Z")

</div>

> [@bradw](#):
>
> body: {“domain”:{“domain”:null,“\_events”:{},“\_eventsCount”:3,“members”:}}

This is what I got back as a response. It suggests to me that the request succeeded but the information there isn’t particularly useful and the lack of anything in my Hosted Mender dashboard would suggest that it wasn’t.

---

<div class="post-metadata">

### Author: ![drewmoseley](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/drewmoseley/32/47_2.png) [@drewmoseley](https://hub.mender.io/u/drewmoseley)
#### Post date: [February 1, 2021, 7:46pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/9 "2021-02-01T19:46:53Z")

</div>

@tranchitella any thoughts on this?

---

<div class="post-metadata">

### Author: ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)
#### Post date: [February 1, 2021, 11:49pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/10 "2021-02-01T23:49:22Z")

</div>

I added a couple of `console.log` for the public and api keys. It looks like they’re getting passed through OK. I had also checked that the values coming in for `identity_data` with `console.log` earlier

 ![Screen Shot 2021-02-01 at 3.40.49 PM](https://canada1.discourse-cdn.com/flex036/uploads/mender/original/2X/f/f672015a9673abcfc550eb34f491fc2d9328cca0.png)

---

<div class="post-metadata">

### Author: ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)
#### Post date: [February 2, 2021, 10:46pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/11 "2021-02-02T22:46:51Z")

</div>

I updated my code to look a little more like the example in the API reference.  
const fetch = require(‘node-fetch’);  
const functions = require(‘firebase-functions’);

```
module.exports = functions.pubsub.topic('menderPreauthorize').onPublish((message) => {
  const deviceId = message.data ? Buffer.from(message.data, 'base64').toString() : null;
  let response
  if (deviceId) {
    const sn = deviceId.split('y')[1]
    const inputBody = {
    "identity_data": {
        "sku": deviceId,
        "sn": sn
      },
      "pubkey": process.env.MENDER_PUBKEY,
    };
    const headers = {
      'Content-Type':'application/json',
      'Accept':'application/json',
      'Authorization':process.env.MENDER_API_KEY
    };
    response = fetch ('https://hosted.mender.io/api/management/v2/devauth/devices',
      {
        method: 'POST',
        body: inputBody,
        headers: headers
      })
      .then((res) => {
        console.log(`body: ${JSON.stringify(res)}`)
        return res.json()
      })
      .catch((err) => {
        console.log(`Error: ${JSON.stringify(err)}`)
        return err
      })
  } else {
    response = 'deviceId was null.'
  }
  return response
})

```

It returned a different response from the `then` block;  
 ![Screen Shot 2021-02-02 at 2.45.33 PM](https://canada1.discourse-cdn.com/flex036/uploads/mender/original/2X/4/40a75b789ff10542cc87cf8301a4a13d9007d8ba.png)

But there is still nothing showing up in the Hosted Mender dashboard.

---

<div class="post-metadata">

### Author: ![Alan](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Alan](https://hub.mender.io/u/Alan)
#### Post date: [February 4, 2021, 10:53am UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/12 "2021-02-04T10:53:51Z")

</div>

Hi @bradw  
Would it be possible that you translate this into copy-pastable `curl` commands?  
This would make reproducing the issue it much simpler.

Be Well,  
Alan

---

<div class="post-metadata">

### Author: ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)
#### Post date: [February 4, 2021, 4:10pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/13 "2021-02-04T16:10:34Z")

</div>

When I follow the Using the APIs tutorial in the docs and reach the “Verify you can call the APIs” section, after calling;

```
curl -H "Authorization: Bearer $JWT" $MENDER_SERVER_URI/api/management/v1/useradm/users | jq '.'

```

I get:

```
zsh: command not found: jq
  % Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload Upload Total Spent Left Speed
100 229 100 229 0 0 190 0 0:00:01 0:00:01 --:--:-- 190
(23) Failed writing body
```

---

<div class="post-metadata">

### Author: ![drewmoseley](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/drewmoseley/32/47_2.png) [@drewmoseley](https://hub.mender.io/u/drewmoseley)
#### Post date: [February 4, 2021, 4:38pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/14 "2021-02-04T16:38:07Z")

</div>

It looks like you don’t have the [“jq” utility](https://stedolan.github.io/jq/download/) installed.

Drew

---

<div class="post-metadata">

### Author: ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)
#### Post date: [February 4, 2021, 6:39pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/15 "2021-02-04T18:39:03Z")

</div>

OK, after I install the `jq` utility. I get the following error;

```
{"error":"failed to decode preauth request: cannot decode public key","request_id":"3d8a6687-8d31-4875-b96e-2448dcb873d5"} **%**

```

When I call;

```
curl -H "Authorization: Bearer $JWT" -H "Content-Type: application/json" -X POST -d "{ \"identity_data\" : $DEVICE_IDENTITY_JSON_OBJECT_STRING, \"pubkey\" : \"$DEVICE_PUBLIC_KEY\" }" $MENDER_SERVER_URI/api/management/v2/devauth/devices
```

---

<div class="post-metadata">

### Author: ![drewmoseley](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/drewmoseley/32/47_2.png) [@drewmoseley](https://hub.mender.io/u/drewmoseley)
#### Post date: [February 4, 2021, 8:26pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/16 "2021-02-04T20:26:07Z")

</div>

OK. That would seem to suggest that the DEVICE\_PUBLIC\_KEY environment variable is not formatted properly for our API. When using the [example from our docs](https://docs.mender.io/server-integration/preauthorizing-devices), the following command is used to format it:

> DEVICE\_PUBLIC\_KEY=“$(cat keys-client-generated/public.key | sed -e :a -e ‘N;s/\n/\n/;ta’)”

Can you compare yours to something generated as above to see if there are any differences?  
Drew

---

<div class="post-metadata">

### Author: ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)
#### Post date: [February 4, 2021, 10:31pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/17 "2021-02-04T22:31:22Z")

</div>

When I run that command from my `/mender` directory, which contains `/keys-client-generated/public.key`, to set it I get;

```
{"error":"failed to decode preauth request: pubkey: non zero value required","request_id":"f4666836-0676-40ae-9c22-906207803cff"} **%**

```

And I enter `$MENDER_PUBLIC_KEY` and indeed it has no value set.

---

<div class="post-metadata">

### Author: ![drewmoseley](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/drewmoseley/32/47_2.png) [@drewmoseley](https://hub.mender.io/u/drewmoseley)
#### Post date: [February 5, 2021, 2:15pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/18 "2021-02-05T14:15:17Z")

</div>

It seems that something is wrong with the public.key file. I just ran the setup from scratch and could not replicate your issue. Can you review the public.key file and make sure it’s correct? The temp one I just generated is as follows:

> -----BEGIN PUBLIC KEY-----  
> MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEA6t3sewLzVf8uVLl0z0pS  
> DcbrHA4/h7exXE2/oQqwuYgBy5RBVTeOpx1YU4mf5yyFzWXxlwVNIcQH3GZgelv5  
> +hBWZZsvbmYgRJTy7cNfQvw7Jit07g+EzOxFwS1SP1gEwuM8ZMDhlim6OiSdl1m+  
> XipnpeXKMzgG2mAdnT5k/13IZIZCq3FVMgIydP7k50PMqmcrR1KycZ9rzl+X333k  
> 1vH6lee2MYDMZmLCuf0oNHapBxGnWqPST4X2LGditREpVEbqt1jUMu68kFHS2ctN  
> +C410WfFeA5XbtEpTYOnVoSKMd898urv/WHxsILbRf1VOeDP2g91YOP0rzkl4RVc  
> kDjB8vLwNIr6AOPeId6KaQK98rd7wjTqZpJXK/XlUyfqs01xurqSDN/1/Yk6sSnf  
> yrbN89Yid8SCS3lx23cByqCvL4+chPgMjo8w0uxkg2a9mzzuJ+q7hHDSKcCLflO9  
> CvfM7T1BYGzMSO2oH4YiaQAgueff8gmzkpkXTfJRVqMFAgMBAAE=  
> -----END PUBLIC KEY-----

---

<div class="post-metadata">

### Author: ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)
#### Post date: [February 5, 2021, 4:10pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/19 "2021-02-05T16:10:52Z")

</div>

My line breaks don’t look like this but otherwise it looks the same.

---

<div class="post-metadata">

### Author: ![drewmoseley](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/drewmoseley/32/47_2.png) [@drewmoseley](https://hub.mender.io/u/drewmoseley)
#### Post date: [February 5, 2021, 7:18pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/20 "2021-02-05T19:18:10Z")

</div>

OK. Can you run just the following command to see what the sed command is producing?

> cat keys-client-generated/public.key | sed -e :a -e ‘N;s/\n/\n/;ta’

In my case (with different key than I posted above) it looks like the following:

```
$ cat keys-client-generated/public.key | sed -e :a -e 'N;s/\n/\\n/;ta'
-----BEGIN PUBLIC KEY-----\nMIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAtWoRQOJ02lraa3hjBnsA\n8gcZL1CWovNVX+tjadl0rX+/LPaVJa6mNRKMY7JXyIiDhL+hm71S3SSWu9iADmDn\nPEfJ9WDkZb60fEOCI69zTmSAkeuETrJyetBk2018GiX9h6FlkFe6tZi8G1W+Ns9C\nLQiH9q8wPSDzIfA+nBvd+hKZn7oqXrIpVxjHoDXqryFOa8C6LLy3Rf0JDAuMJelh\n+1nO7ua3RqckklbxaHgO6QMDozEmEotApW9BvaQOtEVO1hfLm1IC/4DNBUXWRCan\nHaTnba0RIztiJgqeO5dPH82SW32EZ0bxUiITGGxeaqkf+TJyEbN49axqDi+J+wn+\n6sf0PAxHWY1feK/oc0LwqCx6ywmdilyEn4KEsK+dZ0GsY/nPqXeGDLuCcKRC4R1w\nvHghUlXfCM6duI83ItV6P+mW/JyQVn1H5wXkDP/yGAd8Trmvpn8dUZQtTI3/doLJ\n/GhTf/HU6ei+SGEoiIwHT7y9iXwGB2EgVo7jmT3BsMK1AgMBAAE=\n-----END PUBLIC KEY-----

```

What is your host OS? I wonder if the newline format from your sed implementation or some such is different.

Drew

[Next page](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152.md?page=2)
