Package: cloudspot-license-api
Cloudspot API wrapperBasic wrapper for the Cloudspot License API.Use casesAuthenticate and authorize users on an external app, linked to Cloudspot LicenseGetting startedInstallInstall with pip.pipinstallcloudspot-license-apiImportfromcloudspotlicense.apiimportCloudspotLicense_APIFunctionalitiesSetupWhen setting up the class, one parameter is expected: the id of the external application as present on the license server.
This is a crucial and important step. This id is used to determine what application is making the request and what permissions are linked to it.
By using a wrong id, your users will be able to authenticate themselves if their credentials are correct but the permissions will not be mapped correctly. This may lead to giving users too much or too little permissions on the external application.fromcloudspotlicense.apiimportCloudspotLicense_APIapi=CloudspotLicense_API('app-id')Authentication and authorizationAfter setting up the connection, you can use theapito send requests to the Cloudspot License.
Users that are trying to log in will give their username and password. Send this username and password to the License server to validate their credentials.
If correct, the License server will return a token and the user's permissions for the external application. If not correct, aBadCredentialserror will be raised.try:api.authenticate(username,password)exceptBadCredentialsase:print(e)If a request is succesful, you can retrieve the returned token and permissions by usingapi.tokenandapi.permissionsrespectively.Theapi.permissionswill contain anAuthPermissionsobject. This is a list ofAuthPermissionobjects.
AnAuthPermissionobject looks like this:[{"company_id":"COMPANY_ID_1","company_name":"COMPANY_NAME_1","permissions":["use_app"]},{"company_id":"COMPANY_ID_2","company_name":"COMPANY_NAME_2","permissions":["use_xxx","get_xxx","use_app"]}]It contains the permissions for each company the user is part of. Theuse_apppermission is crucial and is always present. If this permission is not present, a user will not be able to authenticate for this company on the external application. A company that hasn't got theuse_apppermission assigned, will not be included in the response.You can loop over the permissions as follows:forcompany_perminapi.permissions.items():print(company_perm.company_id)# Prints the id of the companyprint(company_perm.company_name)# Prints the name of the companyprint(company_perm.permissions)# Prints an array of all the permissions (slugs)forpermincompany_perm.permissions:print('perm: ',perm)# Prints the slug of the permissionRetrieving permissions for a specific company, external application and userYou can retrieve the permissions for an external application for a specific user and company. You can only use this function after succesfully authenticating or supplying a token.fromcloudspotlicense.apiimportCloudspotLicense_API# 1: authenticate firstapi=CloudspotLicense_API('app-id')auth=api.authenticate('email@example.com','my-pwd')permissions=api.get_company_permissions('company-id')print('permissions: ',permissions)# Prints an array of all the permissions (slugs)# 2: OR supply a tokenapi=CloudspotLicense_API('app-id')permissions=api.get_company_permissions('company-id',token='token')print('permissions: ',permissions)# Prints an array of all the permissions (slugs)Retrieving user infoBy default, an emptyUserobject will be attached to theapi. You can retrieve the object withapi.user.
To populate theUser, you need to execute the functionapi.get_user()first.TheUserobject has four attributes:first_name,last_name,emailandcompany.If you've already authenticated the user before using theapi, you do not need to supply a token to the function.
If you're using a newapiobject and want to retrieve the user for a specific token without authenticating first, you can supply the token to the function.If succesful, the user will be attached toapi.userand overwrite any previous user.Retrieve user by authenticating first.api.authenticate(username,password)api.get_user()print(user.first_name)Retrieve user by supplying a token. You can catch the errorNoValidTokento handle a token that is not valid.try:api.get_user(token)exceptNoValidTokenase:print(e)print(user.first_name)