> For the complete documentation index, see [llms.txt](https://docs.sharinpix.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sharinpix.com/documentation/access-and-security/sharinpix-token-verification.md).

# SharinPix Token Verification

This article explains how to verify if a SharinPix Token is valid using an Apex method.

The following sections include:

* [Explanation of the validateToken method](#validatetoken-method)
* [A use case example demonstrating how to call the method in an apex class and validate a Visualforce site token.](#demo)

**For more information on SharinPix tokens, refer to this article:**[Working with SharinPix Tokens](/documentation/best-practices/working-with-sharinpix-tokens.md)

## validateToken Method

The <mark style="color:$danger;">`validateToken (String token)`</mark> method available in the <mark style="color:$danger;">`sharinpix.Client`</mark> class assesses the token's validity. It returns true if the token is valid and false otherwise.This method ensures that only valid and authentic tokens are used to access and upload SharinPix images. The code snippet below demonstrates how to use the validateToken method to validate a SharinPix Token.

### validateToken Method Example

*global void **verifyToken**(String **token**)*

```
Boolean isValid = sharinpix.Client.getInstance().validateToken(token);
System.debug('Token Valid: ' + isValid);
```

## verifyToken Method

The <mark style="color:$danger;">`verifyToken`</mark> method, used inside the <mark style="color:$danger;">`validateToken`</mark> method examines several criteria to ensure the validity of the provided token. It evaluates whether any exceptions should be thrown during the verification process.

### verifyToken Method Example

*global void**verifyToken**(String **token**)*

```
String errorMessage = '';

try {
    sharinpix.Client.getInstance().verifyToken(token);
    return true;
} catch(Exception error) {
    errorMessage = error.getMessage();
}
```

### Criteria for SharinPix Token Verification

The <mark style="color:$danger;">`verifyToken`</mark> method's validation process includes:

* Checking the expiration time (<mark style="color:$danger;">`exp`</mark>) to ensure it's later than the current timestamp, avoiding acceptance of expired tokens.
* Verifying the token issued time (<mark style="color:$danger;">`iat`</mark>) by ensuring that it is earlier than the current timestamp thus preventing acceptance of tokens issued in the future.
* Checking that the token is not null and does not have an invalid format.
* Most importantly, the token header is decoded and an error is thrown if the signature is invalid. This ensures that the token has been created with your credentials only and has not been tampered with.

## Demo

### Validate a SharinPix Token for a Visualforce Site

The sample code below demonstrates the use of the <mark style="color:$danger;">`validateToken`</mark> method for token validation within a Visualforce site. This method is important for ensuring the security and integrity of the authentication process on the Visualforce site.

#### Visualforce Page

```
<apex:page controller="SiteParameterValidateToken">
    <apex:outputPanel rendered="{! canAccessSite }">
        <!-- Component rendered only after Apex validates token. -->
        <p>Valid Token : {! canAccessSite }</p>
    </apex:outputPanel>
    <apex:outputPanel rendered="{! !canAccessSite }">
        <!-- Error message? Redirect? -->
        <p>Valid Token : {! canAccessSite }</p>
    </apex:outputPanel>
</apex:page>
```

#### Apex Class

```apex
public class SiteParameterValidateToken {
    public Boolean canAccessSite { get; set; }
    
    public SiteParameterValidateToken() {
        String token = ApexPages.currentPage().getParameters().get('token');
        canAccessSite = false;
        try {
            canAccessSite = sharinpix.Client.getInstance().validateToken(token); // the new method on SharinPix
        } catch (Exception e) {
            canAccessSite = false;
        }
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.sharinpix.com/documentation/access-and-security/sharinpix-token-verification.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
