Newer Version Available
SessionManagement Class
Namespace
SessionManagement Methods
The following are methods for SessionManagement. All methods are static. Use these methods to customize your two-factor authentication implementation and manage the use of time-based one-time password (TOTP) apps like Google Authenticator with a Salesforce organization. Or, use them to validate a user’s incoming IP address against trusted IP range settings for an organization or profile.
getCurrentSession()
Signature
public static Map<String, String> getCurrentSession()
Usage
Example
The following example shows the name-value pairs in a map returned by getCurrentSession(). Note that UsersId includes an “s” in the name to match the name of the corresponding field in the AuthSession object.
1{
2SessionId=0Ak###############,
3UserType=Standard,
4ParentId=0Ak###############,
5NumSecondsValid=7200,
6LoginType=SAML Idp Initiated SSO,
7LoginDomain=null,
8LoginHistoryId=0Ya###############,
9Username=user@domain.com,
10CreatedDate=Wed Jul 30 19:09:29 GMT 2014,
11SessionType=Visualforce,
12LastModifiedDate=Wed Jul 30 19:09:16 GMT 2014,
13LogoutUrl=https://google.com,
14SessionSecurityLevel=STANDARD,
15UsersId=005###############,
16SourceIp=1.1.1.1
17}getQrCode()
Signature
public static Map<String, String> getQrCode()
Usage
The secret is a base32-encoded string of a 20-byte shared key.
Example
The following is an example of how to request the QR code.
1public String getGetQRCode() {
2 return getQRCode();
3 }
4 public String getQRCode() {
5 Map<String, String> codeResult = Auth.SessionManagement.getQrCode();
6 String result = 'URL: '+codeResult.get('qrCodeUrl') + ' SECRET: ' + codeResult.get('secret');
7 return result;
8 }The following is an example of a returned map.
1{qrCodeUrl=https://www.salesforce.com/secur/qrCode?w=200&h=200&t=tf&u=user%0000000000.com&s=AAAAA7B5BBBB5AAAAAAA66BBBB,
2 secret=AAAAA7B5AAAAAA5BBBBBBBBB66AAA}inOrgNetworkRange(ipAddress)
Signature
public static Boolean inOrgNetworkRange(String ipAddress)
Parameters
- ipAddress
- Type: String
- The IP address to validate.
Return Value
Type: Boolean
Usage
| Trusted IP Range Exists? | User is in the Trusted IP Range? | Return Value |
|---|---|---|
| Yes | Yes | true |
| Yes | No | false |
| No | N/A | false |
isIpAllowedForProfile(profileId, ipAddress)
Signature
public static Boolean isIpAllowedForProfile(String profileId, String ipAddress)
Parameters
Return Value
Type: Boolean
Usage
| Trusted IP Range Exists? | User is in the Trusted IP Range? | Return Value |
|---|---|---|
| Yes | Yes | true |
| Yes | No | false |
| No | N/A | true |
setSessionLevel(level)
Signature
public static Void setSessionLevel(Auth.SessionLevel level)
Parameters
- level
- Type: Auth.SessionLevel
- The session security level to assign to the user. The meaning of each level can be customized in the Session Settings for each organization, such as setting the High Assurance level to apply only to users who authenticated with two-factor authentication or through a specific identity provider.
Return Value
Type: Void
Usage
Example
The following is an example class for setting the session level.
1public class RaiseSessionLevel{
2 public void setLevelHigh() {
3 Auth.SessionManagement.setSessionLevel(Auth.SessionLevel.HIGH_ASSURANCE);
4 }
5 public void setLevelStandard() {
6 Auth.SessionManagement.setSessionLevel(Auth.SessionLevel.STANDARD);
7 }
8}validateTotpTokenForKey(sharedKey, totpCode)
Signature
public static Boolean validateTotpTokenForKey(String sharedKey, String totpCode)
Parameters
Return Value
Type: Boolean
Usage
validateTotpTokenForUser(totpCode)
Signature
public static Boolean validateTotpTokenForUser(String totpCode)
Parameters
- totpCode
- Type: String
- The time-based one-time password (TOTP) code to validate.
Return Value
Type: Boolean