14199f62c5f0778c128d67595b117032e3a7291d
[src/app-framework-binder.git] / doc / afb-application-writing.md
1
2 HOWTO WRITE an APPLICATION above AGL FRAMEWORK
3 ==============================================
4
5 Programmation Languages for Applications
6 -----------------------------------------
7
8 ### Writing an HTML5 application
9
10 Developers of HTML5 applications (client side) can easily create
11 applications for AGL framework using their preferred
12 HTML5 framework.
13
14 Developers may also take advantage of powerful server side plugins to improve
15 application behavior. Server side plugins return an application/json mine-type
16 and can be accessed though either HTTP or Websockets.
17
18 In a near future, JSON-RPC protocol should be added to complete current x-afb-json1 protocol.
19
20 Two examples of HTML5 applications are given:
21
22 - [afb-client](https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-demo.git;a=tree;f=afb-client) a simple "hello world" application template
23
24 - [afm-client](https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-demo.git;a=tree;f=afm-client) a simple "Home screen" application template
25
26 ### Writing a Qt application
27
28 Writing Qt applications is also supported. Qt offers standard API to send request through HTTP or WebSockets.
29
30 It is also possible to write QML applications. A sample QML application [token-websock] is avaliable..
31
32 - [token-websock](https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-binder.git;a=blob;f=test/token-websock.qml)
33 a simple "hello world" application in QML
34
35 ### Writing "C" application
36
37 C applications can use afb-daemon binder through a websocket connection.
38
39 The library **libafbwsc** is provided for C clients that need
40 to connect with an afb-daemon binder.
41
42 The program **afb-client-demo** is the C example that use
43 **libafbwsc** library.
44 Source code is available here
45 [src/afb-client-demo.c](https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-binder.git;a=blob;f=src/afb-client-demo.c).
46
47 Current implementation relies on libsystemd and file descriptors.
48 This model might be review in the future to support secure sockets
49 and free the dependency with libsystemd.
50
51 Handling sessions within applications
52 -------------------------------------
53
54 Applications should understand sessions and tokens management when interacting with afb-daemon binder.
55
56 Applications are communicating with their private binder(afb-daemon) using
57 a network connection or potentially any other connection channel. While current version
58 does not yet implement unix domain this feature might be added in a near future.
59 Developers need to be warn that HTTP protocol is a none connected protocol. This prevents
60 from using HTTP socket connection to authenticate clients.
61
62 For this reason, the binder should authenticate the application
63 by using a shared secret. The secret is named "token" and the identification
64 of client is named "session".
65
66 The examples **token-websock.qml** and **afb-client** are demonstrating
67 how authentication and sessions are managed.
68
69 ### Handling sessions
70
71 Plugins and other binder feature need to keep track of client
72 instances. This is especially important for plugins running as services
73 as they may typically have to keep each client's data separated.
74
75 For HTML5 applications, the web runtime handles the cookie of session
76 that the binder afb-daemon automatically sets.
77
78 Session identifier can be set using the parameter
79 **uuid** or **x-afb-uuid** in URI requests. Within current version of the
80 framework session UUID is supported by both HTTP requests and websocket negotiation.
81
82 ### Exchanging tokens
83
84 At application start, AGL framework communicates a shared secret to both binder
85 and client application. This initial secret is called the "initial token".
86
87 For each of its client application, the binder manages a current active
88 token for session management. This authentication token can be use to restrict
89 access to some plugin's methods.
90
91 The token must be included in URI request on HTTP or during websockets
92 connection using parameter **token** or **x-afb-token**.
93
94 To ensure security, tokens must be refreshed periodically.
95
96 ### Example of session management
97
98 In following examples, we suppose that **afb-daemon** is launched with something equivalent to:
99
100     $ afb-daemon --port=1234 --token=123456 [...]
101
102 making the expectation that **AuthLogin** plugin is requested as default.
103
104 #### Using curl
105
106 First, connects with the initial token, 123456:
107
108     $ curl http://localhost:1234/api/auth/connect?token=123456
109     {
110       "jtype": "afb-reply",
111       "request": {
112          "status": "success",
113          "token": "0aef6841-2ddd-436d-b961-ae78da3b5c5f",
114          "uuid": "850c4594-1be1-4e9b-9fcc-38cc3e6ff015"
115       },
116       "response": {"token": "A New Token and Session Context Was Created"}
117     }
118
119 It returns an answer containing session UUID, 850c4594-1be1-4e9b-9fcc-38cc3e6ff015,
120 and a refreshed token, 850c4594-1be1-4e9b-9fcc-38cc3e6ff015.
121
122 Check if session and token is valid:
123
124     $ curl http://localhost:1234/api/auth/check?token=0aef6841-2ddd-436d-b961-ae78da3b5c5f\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015
125     {
126       "jtype": "afb-reply",
127       "request": {"status":"success"},
128       "response": {"isvalid":true}
129     }
130
131 Refresh the token:
132
133     $ curl http://localhost:1234/api/auth/refresh?token=0aef6841-2ddd-436d-b961-ae78da3b5c5f\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015
134     {
135       "jtype": "afb-reply",
136       "request": {
137          "status":"success",
138          "token":"b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9"
139       },
140       "response": {"token":"Token was refreshed"}
141     }
142
143 Close the session:
144
145     curl http://localhost:1234/api/auth/logout?token=b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015
146     {
147       "jtype": "afb-reply",
148       "request": {"status": "success"},
149       "response": {"info":"Token and all resources are released"}
150     }
151
152 Checking on closed session for uuid should be refused:
153
154     curl http://localhost:1234/api/auth/check?token=b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015
155     {
156       "jtype": "afb-reply",
157       "request": {
158          "status": "failed",
159          "info": "invalid token's identity"
160       }
161     }
162
163 #### Using afb-client-demo
164
165 > The program is packaged within AGL in the rpm **libafbwsc-dev**
166
167 Here is an example of exchange using **afb-client-demo**:
168
169     $ afb-client-demo ws://localhost:1234/api?token=123456
170     auth connect
171     ON-REPLY 1:auth/connect: {"jtype":"afb-reply","request":{"status":"success",
172        "token":"63f71a29-8b52-4f9b-829f-b3028ba46b68","uuid":"5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1"},
173        "response":{"token":"A New Token and Session Context Was Created"}}
174     auth check
175     ON-REPLY 2:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}}
176     auth refresh
177     ON-REPLY 4:auth/refresh: {"jtype":"afb-reply","request":{"status":"success",
178        "token":"8b8ba8f4-1b0c-48fa-962d-4a00a8c9157e"},"response":{"token":"Token was refreshed"}}
179     auth check
180     ON-REPLY 5:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}}
181     auth refresh
182     ON-REPLY 6:auth/refresh: {"jtype":"afb-reply","request":{"status":"success",
183        "token":"e83b36f8-d945-463d-b983-5d8ed73ba529"},"response":{"token":"Token was refreshed"}}
184
185 After closing connection, reconnect as here after:
186
187     $ afb-client-demo ws://localhost:1234/api?token=e83b36f8-d945-463d-b983-5d8ed73ba529\&uuid=5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1 auth check
188     ON-REPLY 1:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}}
189
190 Same connection check using **curl**:
191
192     $ curl http://localhost:1234/api/auth/check?token=e83b36f8-d945-463d-b983-5d8ed73ba529\&uuid=5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1
193     {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}}
194
195 Format of replies
196 -----------------
197
198 Replies use javascript object returned as serialized JSON.
199
200 This object contains at least 2 mandatory fields of name **jtype** and **request**
201 and one optional field of name **response**.
202
203 ### Template
204
205 This is a template of replies:
206
207 ```json
208 {
209    "jtype": "afb-reply",
210    "request": {
211       "status": "success",
212       "info": "informationnal text",
213       "token": "e83b36f8-d945-463d-b983-5d8ed73ba52",
214       "uuid": "5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1",
215       "reqid": "application-generated-id-23456"
216    },
217    "response": ....any response object....
218 }
219 ```
220
221 ### Field jtype
222
223 The field **jtype** must have a value of type string equal to **"afb-reply"**.
224
225 ### Field request
226
227 The field **request** must have a value of type object. This request object
228 has at least one field named **status** and four optional fields named
229 **info**, **token**, **uuid**, **reqid**.
230
231 #### Subfield request.status
232
233 **status** must have a value of type string. This string is equal to **"success"**
234 only in case of success.
235
236 #### Subfield request.info
237
238 **info** is of type string and represent optional information added to the reply.
239
240 #### Subfield request.token
241
242 **token** is of type string. It is sent either at session creation 
243 or when the token is refreshed.
244
245 #### Subfield request.uuid
246
247 **uuid** is of type string. It is sent at session creation.
248
249 #### Subfield request.reqid
250
251 **reqid** is of type string. It is sent in response to HTTP requests
252 that added a parameter of name **reqid** or **x-afb-reqid** at request time.
253 Value returns in the reply has the exact same value as the one received in the request.
254
255 ### Field response
256
257 This field response optionally contains an object returned when request succeeded.
258
259 Format of events
260 ----------------
261
262 Events are javascript object serialized as JSON.
263
264 This object contains at least 2 mandatory fields of name **jtype** and **event**
265 and one optional field of name **data**.
266
267 ### Template
268
269 Here is a template of event:
270
271 ```json
272 {
273    "jtype": "afb-event",
274    "event": "sample_api_name/sample_event_name",
275    "data": ...any event data...
276 }
277 ```
278
279 ### Field jtype
280
281 The field **jtype** must have a value of type string equal to **"afb-event"**.
282
283 ### Field event
284
285 The field **event** carries the event's name.
286
287 The name of the event is made of two parts separated by a slash:
288 the name of the name of the API that generated the event
289 and the name of event within the API.
290
291 ### Field data
292
293 This field data if present holds the data carried by the event.
294