2 HOWTO WRITE an APPLICATION above AGL FRAMEWORK
3 ==============================================
5 Programmation Languages for Applications
6 -----------------------------------------
8 ### Writing an HTML5 application
10 Developers of HTML5 applications (client side) can easily create
11 applications for AGL framework using their preferred
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.
18 In a near future, JSON-RPC protocol should be added to complete current x-afb-json1 protocol.
20 Two examples of HTML5 applications are given:
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
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
26 ### Writing a Qt application
28 Writing Qt applications is also supported. Qt offers standard API to send request through HTTP or WebSockets.
30 It is also possible to write QML applications. A sample QML application [token-websock] is avaliable..
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
35 ### Writing "C" application
37 C applications can use afb-daemon binder through a websocket connection.
39 The library **libafbwsc** is provided for C clients that need
40 to connect with an afb-daemon binder.
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).
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.
51 Handling sessions within applications
52 -------------------------------------
54 Applications should understand sessions and tokens management when interacting with afb-daemon binder.
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.
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".
66 The examples **token-websock.qml** and **afb-client** are demonstrating
67 how authentication and sessions are managed.
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.
75 For HTML5 applications, the web runtime handles the cookie of session
76 that the binder afb-daemon automatically sets.
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.
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".
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.
91 The token must be included in URI request on HTTP or during websockets
92 connection using parameter **token** or **x-afb-token**.
94 To ensure security, tokens must be refreshed periodically.
96 ### Example of session management
98 In following examples, we suppose that **afb-daemon** is launched with something equivalent to:
100 $ afb-daemon --port=1234 --token=123456 [...]
102 making the expectation that **AuthLogin** plugin is requested as default.
106 First, connects with the initial token, 123456:
108 $ curl http://localhost:1234/api/auth/connect?token=123456
110 "jtype": "afb-reply",
113 "token": "0aef6841-2ddd-436d-b961-ae78da3b5c5f",
114 "uuid": "850c4594-1be1-4e9b-9fcc-38cc3e6ff015"
116 "response": {"token": "A New Token and Session Context Was Created"}
119 It returns an answer containing session UUID, 850c4594-1be1-4e9b-9fcc-38cc3e6ff015,
120 and a refreshed token, 850c4594-1be1-4e9b-9fcc-38cc3e6ff015.
122 Check if session and token is valid:
124 $ curl http://localhost:1234/api/auth/check?token=0aef6841-2ddd-436d-b961-ae78da3b5c5f\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015
126 "jtype": "afb-reply",
127 "request": {"status":"success"},
128 "response": {"isvalid":true}
133 $ curl http://localhost:1234/api/auth/refresh?token=0aef6841-2ddd-436d-b961-ae78da3b5c5f\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015
135 "jtype": "afb-reply",
138 "token":"b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9"
140 "response": {"token":"Token was refreshed"}
145 curl http://localhost:1234/api/auth/logout?token=b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015
147 "jtype": "afb-reply",
148 "request": {"status": "success"},
149 "response": {"info":"Token and all resources are released"}
152 Checking on closed session for uuid should be refused:
154 curl http://localhost:1234/api/auth/check?token=b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015
156 "jtype": "afb-reply",
159 "info": "invalid token's identity"
163 #### Using afb-client-demo
165 > The program is packaged within AGL in the rpm **libafbwsc-dev**
167 Here is an example of exchange using **afb-client-demo**:
169 $ afb-client-demo ws://localhost:1234/api?token=123456
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"}}
175 ON-REPLY 2:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}}
177 ON-REPLY 4:auth/refresh: {"jtype":"afb-reply","request":{"status":"success",
178 "token":"8b8ba8f4-1b0c-48fa-962d-4a00a8c9157e"},"response":{"token":"Token was refreshed"}}
180 ON-REPLY 5:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}}
182 ON-REPLY 6:auth/refresh: {"jtype":"afb-reply","request":{"status":"success",
183 "token":"e83b36f8-d945-463d-b983-5d8ed73ba529"},"response":{"token":"Token was refreshed"}}
185 After closing connection, reconnect as here after:
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}}
190 Same connection check using **curl**:
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}}
198 Replies use javascript object returned as serialized JSON.
200 This object contains at least 2 mandatory fields of name **jtype** and **request**
201 and one optional field of name **response**.
205 This is a template of replies:
209 "jtype": "afb-reply",
212 "info": "informationnal text",
213 "token": "e83b36f8-d945-463d-b983-5d8ed73ba52",
214 "uuid": "5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1",
215 "reqid": "application-generated-id-23456"
217 "response": ....any response object....
223 The field **jtype** must have a value of type string equal to **"afb-reply"**.
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**.
231 #### Subfield request.status
233 **status** must have a value of type string. This string is equal to **"success"**
234 only in case of success.
236 #### Subfield request.info
238 **info** is of type string and represent optional information added to the reply.
240 #### Subfield request.token
242 **token** is of type string. It is sent either at session creation
243 or when the token is refreshed.
245 #### Subfield request.uuid
247 **uuid** is of type string. It is sent at session creation.
249 #### Subfield request.reqid
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.
257 This field response optionally contains an object returned when request succeeded.
262 Events are javascript object serialized as JSON.
264 This object contains at least 2 mandatory fields of name **jtype** and **event**
265 and one optional field of name **data**.
269 Here is a template of event:
273 "jtype": "afb-event",
274 "event": "sample_api_name/sample_event_name",
275 "data": ...any event data...
281 The field **jtype** must have a value of type string equal to **"afb-event"**.
285 The field **event** carries the event's name.
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.
293 This field data if present holds the data carried by the event.