Newer Version Available
AuthRequestHandler Interface
Use this interface to handle authorization request responses.
Namespace
Usage
When using this interface, the following limits are overridden. See Execution Governors and Limits in the Apex Developer Guide.
| Limit Name | Overridden Value |
|---|---|
| Total number of SOQL queries issued | 2 |
| Total number of records retrieved by a single SOSL query | 2 |
| Total number of DML statements issued | 1 |
| Total number of records processed as a result of DML statements | 1 |
| Total number of callouts (HTTP requests or web services calls) in a transaction | 2 |
AuthRequestHandler Methods
The following are methods for AuthRequestHandler.
handleAuthRequest(var1)
Handles authorization request response.
Signature
public RichMessaging.AuthRequestResult handleAuthRequest(RichMessaging.AuthRequestResponse var1)
Parameters
- var1
- Type: RichMessaging.AuthRequestResponse
- The authorization response.
Return Value
AuthRequestHandler Example Implementation
This is an example implementation of the RichMessaging.AuthRequestHandler interface.
1global class SampleAuthRequestHandler implements RichMessaging.AuthRequestHandler {
2
3 global RichMessaging.AuthRequestResult handleAuthRequest(RichMessaging.AuthRequestResponse authReqResponse) {
4
5 // Get contact email from messaging session
6 String sessionId = authReqResponse.getContextRecordId();
7 String contactEmail = [select MessagingSession.EndUserContact.Email from MessagingSession where id = :sessionId].EndUserContact.Email;
8
9 RichMessaging.AuthRequestResultStatus authRequestStatus = RichMessaging.AuthRequestResultStatus.DECLINED;
10 DateTime dt = DateTime.now();
11
12 // Get user info if there's a valid contact email
13 if (!String.isBlank(contactEmail)) {
14 String userInfoUrl = 'https://api.MY_AUTH_DOMAIN.com/v1/';
15
16 HttpRequest req = new HttpRequest();
17
18 req.setEndpoint(userInfoUrl);
19 req.setHeader('Content-Type','application/json');
20 req.setMethod('GET');
21 req.setHeader('Authorization', 'Bearer '+authReqResponse.getAccessToken());
22 Http http = new Http();
23 HTTPResponse res = http.send(req);
24
25 String responseBody = res.getBody();
26
27 UserWrapper userInfo = (UserWrapper)System.JSON.deserialize(responseBody, UserWrapper.class);
28
29 if (userInfo.email == contactEmail) {
30 authRequestStatus = RichMessaging.AuthRequestResultStatus.AUTHENTICATED;
31 dt = dt.addHours(6);
32 }
33 }
34
35 return new RichMessaging.AuthRequestResult(
36 null,
37 authRequestStatus,
38 dt);
39 }
40
41 public class UserWrapper{
42 public String href;
43 public String display_name;
44 public String type;
45 public String country;
46 public String product;
47 public String email;
48 }
49}