Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add java docs #4

Merged
merged 1 commit into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 179 additions & 0 deletions docs/reference/SDKs/java.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
---
sidebar_position: 3
title: Java
---

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

# Java

## Add the SDK to your Java application

Init the Java SDK and check for permissions.

1. Install the Permit.io SDK:

<Tabs>
<TabItem value="maven" label="with Maven" default>

For [Maven](https://maven.apache.org/) projects, use:

```xml
<dependency>
<groupId>io.permit</groupId>
<artifactId>permit-sdk-java</artifactId>
<version>0.0.11</version>
</dependency>
```

</TabItem>
<TabItem value="gradle" label="with Gradle">

For [Gradle](https://gradle.org/) projects, configure `permit-sdk-java` as a dependency in your `build.gradle`:

```groovy
dependencies {
// ...

implementation 'io.permit:permit-sdk-java:0.0.11'
}
```

</TabItem>
</Tabs>

2. Create a new instance of the Permit SDK in your code:
You can find instructions on getting an API key [here](../../tutorials/quickstart#2-get-permitio-api-key)

```java
import io.permit.sdk.Permit;
import io.permit.sdk.PermitConfig;

// This line initializes the SDK and connects your Java app
// to the Permit.io PDP container you've set up in the previous step.
Permit permit = new Permit(
new PermitConfig.Builder("[YOUR_API_KEY]")
// in production, you might need to change this url to fit your deployment
.withPdpAddress("http://localhost:7000")
.build()
);
```

3. Sync the user to the permissions system

When the user first logins, and after you check if he authenticated successfully (i.e: by checking the JWT)
you need to declare the user in the permission system so you can run `permit.check()` on that user:

```java
// init your user (only needed ONCE, after the user first logins successfully)
User user = new User.Builder("[A_USER_ID]")
.withEmail("johndoe@permit.io")
.withFirstName("John")
.withLastName("Doe")
// you can assign role to the user at login
.withRoles(new ArrayList<>(List.of(new AssignedRole[]{
new AssignedRole("roleId", "tenantId"),
// example assign the user (at login) to the default tenant with an admin role
new AssignedRole("admin", "default"),
})))
.build();

// You need to call sync user in order to create the user in the
// permission system and be able to ask permit.check() on that user
permit.api.syncUser(user);
```

4. Check for permissions:

```java
import io.permit.sdk.enforcement.Resource;
import io.permit.sdk.enforcement.User;

boolean permitted = permit.check(
user,
"create",
Resource.fromString("document")
);

if (permitted) {
System.out.println("User is PERMITTED to create a document");
} else {
System.out.println("User is NOT PERMITTED to create a document");
}
```

## Full app example

Assuming a Spring Boot app with a single file, with the SDK installed:

```java
package com.example.myproject;

import io.permit.sdk.Permit;
import io.permit.sdk.PermitConfig;
import io.permit.sdk.api.PermitApiException;
import io.permit.sdk.enforcement.Resource;
import io.permit.sdk.enforcement.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@SpringBootApplication
public class DemoApplication {
// You can open http://localhost:4000 to invoke this http
// endpoint, and see the outcome of the permission check.
@GetMapping("/")
ResponseEntity<String> home() throws IOException, PermitApiException {
// init the permit SDK
Permit permit = new Permit(
new PermitConfig.Builder("[YOUR_API_KEY]")
// in production, you might need to change this url to fit your deployment
.withPdpAddress("http://localhost:7000")
.build()
);

// init your user (only needed ONCE, after the user first logins successfully)
User user = new User.Builder("[A_USER_ID]")
.withEmail("johndoe@permit.io")
.withFirstName("John")
.withLastName("Doe")
.withRoles(new ArrayList<>(List.of(new AssignedRole[]{
new AssignedRole("roleId", "tenantId"),
// assign the user the admin role inside the default tenant
new AssignedRole("admin", "default"),
})))
.build();

// You need to call sync user in order to create the user in the
// permission system and be able to ask permit.check() on that user
permit.api.syncUser(user);

boolean permitted = permit.check(
user, // you may also call User.fromString("[A_USER_ID]"),
"create",
Resource.fromString("document")
);

if (permitted) {
return ResponseEntity.status(HttpStatus.OK).body(
"User is PERMITTED to create a document"
);
} else {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(
"User is NOT PERMITTED to create a document"
);
}
}

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
8 changes: 4 additions & 4 deletions docs/reference/SDKs/nodejs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const { Permit } = require("permitio");
```

3. Create a new instance of the SDK:
You can find instructions on getting an API key [here](../../tutorials/quickstart#2-get-permitio-api-key)
You can find instructions on getting an API key [here](../../tutorials/quickstart#2-get-permitio-api-key)

```js
// This line initializes the SDK and connects your Node.js app
Expand All @@ -40,9 +40,9 @@ const permit = new Permit({
```js
const permitted = await permit.check(user.id, "create", "document");
if (permitted) {
console.log("User is permitted to create a document");
console.log("User is PERMITTED to create a document");
} else {
console.log("is NOT PERMITTED to create document!");
console.log("User is NOT PERMITTED to create a document");
}
```

Expand Down Expand Up @@ -72,7 +72,7 @@ app.get("/", async (req, res) => {
// Example user object
// You would usually get the user from your authenticantion layer (e.g. Auth0, Cognito, etc) via a JWT token or a database.
const user = {
id: "[A_USER_ID]",]]",
id: "[A_USER_ID]",
firstName: "John",
lastName: "Doe",
email: "johndoe@permit.io",
Expand Down
1 change: 1 addition & 0 deletions docs/tutorials/quickstart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ docker run \

1. [Node.js](/reference/SDKs/nodejs)
2. [Python](/reference/SDKs/python)
3. [Java](/reference/SDKs/java)

more SDKs coming soon...
1 change: 1 addition & 0 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const config = {
prism: {
theme: lightCodeTheme,
darkTheme: darkCodeTheme,
additionalLanguages: ["java", "csharp", "groovy"],
},
//search configuration
...searchConfig,
Expand Down