|
| 1 | +--- |
| 2 | +id: machine_to_machine |
| 3 | +title: Machine to Machine |
| 4 | +sidebar_label: Machine to Machine |
| 5 | +slug: /guides/howto/machine-to-machine |
| 6 | +description: How to programatically access a machine from a machin. |
| 7 | +--- |
| 8 | + |
| 9 | +A service (aka: _machine_) is a non human program that may request an access token from _Crossid_ in order to authenticate to other services. |
| 10 | + |
| 11 | +A good example is a micro service or a schedueld job that requires access to a protected REST API. |
| 12 | + |
| 13 | +This how-to explains how to perform authentication programmatically, with no user interaction, so a service could access some API. |
| 14 | + |
| 15 | +### Create a service account |
| 16 | + |
| 17 | +A service account is a user intended to be used for services rather people. |
| 18 | + |
| 19 | +Lets create a service account that will be granted with privileges to access our API. |
| 20 | + |
| 21 | +import Tabs from "@theme/Tabs"; |
| 22 | +import TabItem from "@theme/TabItem"; |
| 23 | + |
| 24 | +<Tabs |
| 25 | +defaultValue="console" |
| 26 | +values={[ |
| 27 | +{label: 'Console', value: 'console'}, |
| 28 | +{label: 'Curl', value: 'curl'} |
| 29 | +]}> |
| 30 | +<TabItem value="console"> |
| 31 | + |
| 32 | +1. In Admin console, navigate to <b>Directory → Service Accounts</b>. |
| 33 | +1. Open the Actions dropdown and click <b>Add</b> |
| 34 | +1. Follow the modal (don't forget to make the account active). |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +</TabItem> |
| 39 | +<TabItem value="curl"> |
| 40 | + |
| 41 | +```bash {10-11,18-22} |
| 42 | +curl -X POST \ |
| 43 | +-H "Authorization: Bearer <API_TOKEN>" \ |
| 44 | +-d ' |
| 45 | +{ |
| 46 | + "userName": "periodicCleanup", |
| 47 | + "displayName": "Periodic Cleanup Account", |
| 48 | + "active": true |
| 49 | +} |
| 50 | +' https://{tenant}.crossid.io/api/v1/resources/cid/ServiceAccount?reason=add-user |
| 51 | +``` |
| 52 | + |
| 53 | +</TabItem> |
| 54 | +</Tabs> |
| 55 | + |
| 56 | +### Machine to Machine |
| 57 | + |
| 58 | +Thie machine to machine integration will make our service account be able to authenticate via OAuth2. |
| 59 | + |
| 60 | +<Tabs |
| 61 | +defaultValue="console" |
| 62 | +values={[ |
| 63 | +{label: 'Console', value: 'console'}, |
| 64 | +]}> |
| 65 | +<TabItem value="console"> |
| 66 | + |
| 67 | +1. In Admin console, navigate to <b>Marketplace → Machine to Machine</b>. |
| 68 | +1. Click the <b>Add Integration</b> button. |
| 69 | +1. Follow the wizard. |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +</TabItem> |
| 74 | +</Tabs> |
| 75 | + |
| 76 | +Copy the _Client ID_ and _Client Secret_ for the next steps. |
| 77 | + |
| 78 | +### Create an API integration |
| 79 | + |
| 80 | +Lets create an API that our service should access. |
| 81 | + |
| 82 | +<Tabs |
| 83 | +defaultValue="console" |
| 84 | +values={[ |
| 85 | +{label: 'Console', value: 'console'}, |
| 86 | +]}> |
| 87 | +<TabItem value="console"> |
| 88 | + |
| 89 | +1. In Admin console, navigate to <b>Marketplace → API</b>. |
| 90 | +1. Click the <b>Add Integration</b> button. |
| 91 | +1. Follow the wizard. |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +</TabItem> |
| 96 | +</Tabs> |
| 97 | + |
| 98 | +### Grant Access |
| 99 | + |
| 100 | +We have to grant our service account access to the API. |
| 101 | + |
| 102 | +### Authenticate |
| 103 | + |
| 104 | +At this point, we have a service account that have _write_ grants to access our API app, lets authenticate. |
| 105 | + |
| 106 | +1. Replace <client_id> with the ID from step 2 |
| 107 | +1. Replace <client_secret> with the Secret from step 2 |
| 108 | + |
| 109 | +```bash |
| 110 | +curl -X POST https://{tenant}.crossid.io/oauth2/token \ |
| 111 | + -F grant_type=client_credentials \ |
| 112 | + -F client_id=<client_id> \ |
| 113 | + -F client_secret=<client_secret> \ |
| 114 | + -F scope='write' |
| 115 | +``` |
| 116 | + |
| 117 | +Output: |
| 118 | + |
| 119 | +```json |
| 120 | +{ |
| 121 | + "access_token": "eyJhbGciOiJSUzI1NiIsImt...", |
| 122 | + "expires_in": 3599, |
| 123 | + "refresh_expires_in": 2592000000000000, |
| 124 | + "scope": "write", |
| 125 | + "token_type": "bearer" |
| 126 | +} |
| 127 | +``` |
0 commit comments