1 # How to write an application on top of AGL FRAMEWORK
3 ## Programming Languages for Applications
5 ### Writing an HTML5 application
7 Developers of HTML5 applications (client side) can easily create
8 applications for AGL framework using their preferred
11 Developers may also take advantage of powerful server side bindings to improve
13 Server side bindings return an application/json mine-type
14 and can be accessed though either HTTP or Websockets.
16 In a near future, JSON-RPC protocol should be added to complete the current
19 Two examples of HTML5 applications are given:
21 - [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 - [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 ### Writing a Qt application
27 Writing Qt applications is also supported.
28 Qt offers standard API to send request through HTTP or WebSockets.
30 It is also possible to write QML applications.
31 A sample QML application [token-websock] is available:
33 - [token-websock](https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-binder.git;a=blob;f=test/token-websock.qml)
35 A simple "hello world" application in QML
37 ### Writing a "C" application
39 C applications can use afb-daemon binder through a websocket connection.
41 The library **libafbwsc** is provided for C clients that need
42 to connect with an afb-daemon binder.
44 The program **afb-client-demo** is the C example that uses the
45 **libafbwsc** library.
46 Source code is available here
47 [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).
49 Current implementation relies on libsystemd and file descriptors.
50 This model may be reviewed in the future to support secure sockets
51 and get rid of libsystemd dependency.
53 ### Handling sessions within applications
55 Applications should understand sessions and token management when interacting
56 with afb-daemon binder.
58 Applications communicate with their private binder (afb-daemon) using
59 a network connection or any other potential connection channel.
60 While the current version does not yet implement Unix socket,
61 this feature might be added in the near future.
62 Developers need to be warn that HTTP protocol is a none
63 connected protocol and that using HTTP socket connection to authenticate
64 clients is not supported.
66 For this reason, the binder should authenticate the application
67 by using a shared secret.
68 The secret is named "token" and the identification of client is named "session.”
70 The examples **token-websock.qml** and **afb-client** are demonstrating
71 how authentication and sessions are managed.
75 Bindings and other binder features need to keep track of client
77 This is especially important for bindings running as services
78 as they may typically have to keep each client's data separated.
80 For HTML5 applications, the web runtime handles the cookie of the session
81 that the binder afb-daemon automatically sets.
83 Session identifier can be set using the parameter **uuid** or **x-afb-uuid** in
85 Within current version of the framework session UUID is supported
86 by both HTTP requests and websocket negotiation.
90 At application start, AGL framework communicates a shared secret to both binder
91 and client application.
92 This initial secret is called the "**initial token**".
94 For each of its client application, the binder manages a current active
95 token for session management.
96 This authentication token can be use to restrict the access to some binding's methods.
98 The token must be included in URI request on HTTP or during websockets
99 connection using parameter **token** or **x-afb-token**.
101 To ensure security, tokens must be refreshed periodically.
103 ### Example of session management
105 In following examples, we suppose that **afb-daemon** is launched with something
109 afb-daemon --port=1234 --token=123456 [...]
112 making the expectation that **AuthLogin** binding is requested as default.
116 First, connects with the initial token, 123456:
119 $ curl http://localhost:1234/api/auth/connect?token=123456
121 "jtype": "afb-reply",
124 "token": "0aef6841-2ddd-436d-b961-ae78da3b5c5f",
125 "uuid": "850c4594-1be1-4e9b-9fcc-38cc3e6ff015"
127 "response": {"token": "A New Token and Session Context Was Created"}
131 It returns an answer containing session UUID, 850c4594-1be1-4e9b-9fcc-38cc3e6ff015,
132 and a refreshed token, 850c4594-1be1-4e9b-9fcc-38cc3e6ff015.
134 Check if session and token is valid:
137 $ curl http://localhost:1234/api/auth/check?token=0aef6841-2ddd-436d-b961-ae78da3b5c5f\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015
139 "jtype": "afb-reply",
140 "request": {"status":"success"},
141 "response": {"isvalid":true}
148 $ curl http://localhost:1234/api/auth/refresh?token=0aef6841-2ddd-436d-b961-ae78da3b5c5f\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015
150 "jtype": "afb-reply",
153 "token":"b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9"
155 "response": {"token":"Token was refreshed"}
162 $ curl http://localhost:1234/api/auth/logout?token=b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015
164 "jtype": "afb-reply",
165 "request": {"status": "success"},
166 "response": {"info":"Token and all resources are released"}
170 Checking on closed session for uuid should be refused:
173 $ curl http://localhost:1234/api/auth/check?token=b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015
175 "jtype": "afb-reply",
178 "info": "invalid token's identity"
183 #### Using afb-client-demo
185 - The program is packaged within AGL in the rpm **libafbwsc-dev**
187 Here is an example of exchange using **afb-client-demo**:
190 $ afb-client-demo ws://localhost:1234/api?token=123456
192 ON-REPLY 1:auth/connect: {"jtype":"afb-reply","request":{"status":"success",
193 "token":"63f71a29-8b52-4f9b-829f-b3028ba46b68","uuid":"5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1"},
194 "response":{"token":"A New Token and Session Context Was Created"}}
196 ON-REPLY 2:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}}
198 ON-REPLY 4:auth/refresh: {"jtype":"afb-reply","request":{"status":"success",
199 "token":"8b8ba8f4-1b0c-48fa-962d-4a00a8c9157e"},"response":{"token":"Token was refreshed"}}
201 ON-REPLY 5:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}}
203 ON-REPLY 6:auth/refresh: {"jtype":"afb-reply","request":{"status":"success",
204 "token":"e83b36f8-d945-463d-b983-5d8ed73ba529"},"response":{"token":"Token was refreshed"}}
207 After closing connection, reconnect as here after:
210 $ afb-client-demo ws://localhost:1234/api?token=e83b36f8-d945-463d-b983-5d8ed73ba529\&uuid=5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1 auth check
211 ON-REPLY 1:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}}
214 Same connection check using **curl**:
217 $ curl http://localhost:1234/api/auth/check?token=e83b36f8-d945-463d-b983-5d8ed73ba529\&uuid=5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1
218 {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}}
221 ### Format of replies
223 Replies use javascript object returned as serialized JSON.
225 This object contains at least 2 mandatory fields of name **jtype** and
226 **request** and one optional field of name **response**.
228 #### Template of replies
230 This is a template of replies:
234 "jtype": "afb-reply",
237 "info": "informationnal text",
238 "token": "e83b36f8-d945-463d-b983-5d8ed73ba52",
239 "uuid": "5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1",
240 "reqid": "application-generated-id-23456"
242 "response": ....any response object....
246 #### Field jtype of replies
248 The field **jtype** must have a value of type string equal to **"afb-reply"**.
250 #### Field request of replies
252 The field **request** must have a value of type object.
253 This request object has at least one field named **status**
254 and four optional fields named **info**, **token**, **uuid**, **reqid**.
256 ##### Subfield request.status
258 **status** must have a value of type string. This string is equal to **"success"**
259 only in case of success.
261 ##### Subfield request.info
263 **info** is of type string and represent optional information added to the reply.
265 ##### Subfield request.token
267 **token** is of type string. It is sent either at session creation
268 or when the token is refreshed.
270 ##### Subfield request.uuid
272 **uuid** is of type string. It is sent at session creation.
274 ##### Subfield request.reqid
276 **reqid** is of type string. It is sent in response to HTTP requests
277 that added a parameter of name **reqid** or **x-afb-reqid** at request time.
278 Value returns in the reply has the exact same value as the one received in the
281 #### Field response of replies
283 This field response optionally contains an object returned when request
288 Events are javascript object serialized as JSON.
290 This object contains at least 2 mandatory fields of name **jtype** and **event**
291 and one optional field of name **data**.
293 #### Template of event
295 Here is a template of event:
299 "jtype": "afb-event",
300 "event": "sample_api_name/sample_event_name",
301 "data": ...any event data...
305 #### Field jtype of event
307 The field **jtype** must have a value of type string equal to **"afb-event"**.
309 #### Field event of event
311 The field **event** carries the event's name.
313 The name of the event is made of two parts separated by a slash:
314 the name of the name of the API that generated the event
315 and the name of event within the API.
317 #### Field data of event
319 This field data if present holds the data carried by the event.