https://github.com/authress/authress-sdk.kt
The Authress SDK for Kotlin/JVM provides authorization as a service with fully compatible REST apis.
https://github.com/authress/authress-sdk.kt
Last synced: 11 months ago
JSON representation
The Authress SDK for Kotlin/JVM provides authorization as a service with fully compatible REST apis.
- Host: GitHub
- URL: https://github.com/authress/authress-sdk.kt
- Owner: Authress
- License: apache-2.0
- Created: 2021-06-21T10:28:33.000Z (almost 5 years ago)
- Default Branch: release/0.1
- Last Pushed: 2024-10-29T13:20:44.000Z (over 1 year ago)
- Last Synced: 2025-05-13T06:43:47.001Z (about 1 year ago)
- Language: Kotlin
- Size: 77.1 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Authress SDK for Kotlin, Java, and the JVM
This is the Authress SDK used to integrate with the authorization as a service provider Authress at https://authress.io.
## Getting Started
The package is published in the [Github maven repository](https://github.com/Authress/authress-sdk.kt/packages/879275) @ https://maven.pkg.github.com/Authress/authress-sdk.kt/
```
dependencies {
implementation("authress:authress-sdk")
}
```
* Maven - add the following to your pom.xml:
```xml
github-repository
https://maven.pkg.github.com/Authress/authress-sdk.kt/
io.authress
authress-sdk
[0.1.24,)
```
### Kotlin example
#### Authorize using a user token
```kotlin
import io.authress.client.*
// create an instance of the API class during service initialization
// Replace ACCOUNT_ID with the Authress accountId
const authressClient = AuthressClient("https://ACCOUNT_ID.api-REGION.authress.io")
// on api route
// [route('/resources/')]
fun getResource(resourceId: String) {
// Get the user token and pass it to authress
const authorizationToken = request.headers.get('authorization')
authressClient.setToken(authorizationToken)
// Check Authress to authorize the user
try {
authressClient.userPermissions.authorizeUser(userId, "resources/$resourceId", "READ")
} catch (e: ClientException) {
// User doesn't have access
return 404
} catch (e: ServerException) {
println("5xx response calling UserPermissionsApi#authorizeUser")
return 503
}
// On success, continue with the route code to load resource and return it
// ...
return 200
}
```
#### Authorize with a service client
```kotlin
import io.authress.client.*
// create an instance of the API class during service initialization
// Replace AccountId with the Authress domain for your account
// Create a service client in the Authress management portal and past the access token here
// This will generate a token automatically instead of passing the user token to the api
const accessToken = 'eyJrZXlJ....'
const authressClient = AuthressClient("https://ACCOUNT_ID.api-REGION.authress.io", ServiceClientTokenProvider(accessToken))
// on api route
// [route('/resources/')]
fun getResource(resourceId: String) {
// Check Authress to authorize the user
try {
authressClient.userPermissions.authorizeUser(userId, "resources/$resourceId", "READ")
} catch (e: ClientException) {
// User doesn't have access
return 404
} catch (e: ServerException) {
println("5xx response calling UserPermissionsApi#authorizeUser")
return 503:
}
// On success, continue with the route code to load resource and return it
// ...
return 200
}
```
#### Verify Authress JWTs
```kotlin
val accessToken = "eyJhbGciOiJFZ.encodedToken.SignatureAyJhbGciOiJFZ"
val tokenVerifier = TokenVerifier("https://YourCustomDomain")
// Or using the accountId
// val tokenVerifier = TokenVerifier("https://ACCOUNT_ID.login.authress.io")
try {
tokenVerifier.verify(accessToken)
} catch (e: ClientException) {
println("Invalid Token: $e")
throw e
}
```
## Contribution
### Requires
* Java: https://openjdk.java.net/install
* Kotlin 1.4.30
` sudo snap install kotlin --classic`
* Gradle 7.1
Run
```
gradle wrapper
./gradlew build
./gradlew check assemble
```
(This runs all tests and packages the library.)