{"info":{"_postman_id":"33eebca2-2c5b-4b07-9bd6-8b7d34a80a0a","name":"smzero® SSO","description":"<html><head></head><body><h2 id=\"disclaimer\">Disclaimer</h2>\n<p>All information contained in this documentation must be kept confidential. To use this integration, you must follow our <a href=\"https://smze.ro/terms\">terms of use</a> and be aware of our <a href=\"https://smze.ro/privacy\">privacy</a> and <a href=\"https://smze.ro/cookies\">cookie</a> policies. The smzero® API was developed according to <a href=\"https://hitrustalliance.net/hitrust-framework\">HITRUST CSF</a> standards, for the highest levels of requirements and certifications on the market.</p>\n<h2 id=\"about-of-this-api\">About of this API</h2>\n<p>Now smzero Users can login in Third Party Applications and smzero Connect services using the same login of smzero.</p>\n<p>To use smzero authentication, you must first create an application within smzero, for this you have application and integrations administrator role and create an OAuth 2.0 application, with the name of the service that will authenticate with smzero.</p>\n<p>If it is a smzero Connect, this step has probably already been completed. Then we will move on to the authentication step.</p>\n</body></html>","schema":"https://schema.getpostman.com/json/collection/v2.0.0/collection.json","toc":[],"owner":"5536699","collectionId":"33eebca2-2c5b-4b07-9bd6-8b7d34a80a0a","publishedId":"2sA3e2e9K8","public":true,"customColor":{"top-bar":"FFFFFF","right-sidebar":"303030","highlight":"0099ff"},"publishDate":"2024-07-08T15:50:57.000Z"},"item":[{"name":"Get Started","id":"9ce24ae7-2db7-445a-81fe-c4a91b069edb","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"REDIRECT","header":[],"url":"https://auth.smzero.com.br/","description":"<h1 id=\"starting-authentication\">Starting authentication</h1>\n<p>This is the first step to create an <strong>Auth</strong> using smzero SSO. Above is a code example of how to implement this redirection in your application.</p>\n<p>We will use some <code>const</code> to declare some items in this example, we strongly recommend setting it as environment variables:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-javascript\">const keyName = 'smzero_auth';\nconst smzeroAuthUrl = 'https://auth.smzero.com.br/';\nconst smzeroApiUrl = 'https://api.smzero.com.br/auth/isTokenValid';\nconst token = localStorage.getItem(keyName);\n\n</code></pre>\n<h3 id=\"check-if-you-have-a-token\">Check if you have a token</h3>\n<p>Let's do some validations, to see if this user already has a valid token, if not, redirect to smzero Auth URL, for create:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-javascript\">if (!token) {\n    window.location.href = smzeroAuthUrl;\n}\n\n</code></pre>\n<blockquote>\n<p>Note: for smzero Connect, after your redirect, for <code>https://auth.smzero.com.br/</code> integration, the user must log in and after logging in, within <strong>smzero Clinic Manager</strong> they must choose the option to access <strong>smzero Connect</strong>. After this user will be redirected to configured URI, <code>https://</code><strong><code>{redirectUri}</code></strong><code>/auth?token=</code>, now get the token and save. </p>\n</blockquote>\n<h3 id=\"if-you-have-continue-your-flow\">If you have, continue your flow</h3>\n<p>Now, if he has a valid token saved in local storage, allow to log in. This is an example of how to handle if the user already has a valid token:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-javascript\">else\n {\n    $.ajax({\n        url: smzeroApiUrl,\n        type: 'post',\n        headers: {\n            Authorization: 'Bearer ' + token,\n        },\n        success: function (data) {\n            localStorage.setItem(keyName, token);\n            window.location.href = '/'; // home of your web app\n        },\n        fail: function () {\n            window.location.href = smzeroAuthUrl;\n        }\n    });\n}\n\n</code></pre>\n<p>This request has a example here: <a href=\"#f6205d55-818d-4bd0-a21f-c2b0fcf7eb8f\">(token-validation)</a></p>\n<h3 id=\"final-code\">Final code</h3>\n<p>When the deployment is finished, your code should look like the example below:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-javascript\">&lt;html xmlns=\"http://www.w3.org/1999/xhtml\"&gt;\n&lt;head&gt;\n    &lt;title&gt;&lt;/title&gt;\n    &lt;script&gt;\n        const keyName = 'smzero_auth';\n        const smzeroAuthUrl = 'https://auth.smzero.com.br/';\n        const smzeroApiUrl = 'https://api.smzero.com.br/auth/isTokenValid';\n        const token = localStorage.getItem(keyName);\n        if (!token) {\n            window.location.href = smzeroAuthUrl;\n        }\n        else\n        {\n            $.ajax({\n                url: smzeroApiUrl,\n                type: 'post',\n                headers: {\n                    Authorization: 'Bearer ' + token,\n                },\n                success: function (data) {\n                    localStorage.setItem(keyName, token);\n                    window.location.href = '/'; // home of your web app\n                },\n                fail: function () {\n                    window.location.href = smzeroAuthUrl;\n                }\n            });\n        }\n    &lt;/script&gt;\n&lt;/head&gt;\n&lt;body&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n\n</code></pre>\n<h1 id=\"validate-token-for-requests\">Validate Token for Requests</h1>\n<p>When making requests to smzero, you will need a decode the JWT, this can be done directly as in the example below or using some library, we recommend <a href=\"https://www.npmjs.com/package/jwt-decode\">jwt-decode</a>.</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-javascript\">function parseJwt(token) {\n    const base64Url = token.split('.')[1];\n    const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');\n    const jsonPayload = decodeURIComponent(window.atob(base64).split('').map(function (c) {\n        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);\n    }).join(''));\n    return JSON.parse(jsonPayload);\n}\n$(function () {\n    const token = parseJwt(localStorage.getItem('smzero_auth'));\n    $('#username').text(token.unique_name);\n});\n\n</code></pre>\n","urlObject":{"protocol":"https","path":[""],"host":["auth","smzero","com","br"],"query":[],"variable":[]}},"response":[],"_postman_id":"9ce24ae7-2db7-445a-81fe-c4a91b069edb"},{"name":"Token Validate","id":"f6205d55-818d-4bd0-a21f-c2b0fcf7eb8f","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"auth":{"type":"bearer","bearer":{"basicConfig":[{"key":"token","value":"<token>"}]},"isInherited":false},"method":"POST","header":[],"url":"https://api.smzero.com.br/auth/isTokenValid","description":"<p>This is the request used to validate the saved token every time the user enters the application.</p>\n","urlObject":{"protocol":"https","path":["auth","isTokenValid"],"host":["api","smzero","com","br"],"query":[],"variable":[]}},"response":[],"_postman_id":"f6205d55-818d-4bd0-a21f-c2b0fcf7eb8f"}]}