Skip to content

Commit 15e58f5

Browse files
authored
Merge branch 'master' into raz/per-9686-add-sdks-banners-for-all-opensources
2 parents 42d6a85 + 596f372 commit 15e58f5

276 files changed

Lines changed: 24875 additions & 610 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/publish.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ jobs:
1212
packages: write
1313
steps:
1414
- uses: actions/checkout@v2
15-
- uses: actions/setup-java@v2
15+
- uses: actions/setup-java@v3.11.0
1616
with:
17-
java-version: '11'
18-
distribution: 'adopt'
17+
java-version: '8'
18+
distribution: 'corretto'
1919
- name: Validate Gradle wrapper
2020
uses: gradle/wrapper-validation-action@v1
2121
- name: Publish package

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77

88
# Ignore Gradle build output directory
99
build
10+
11+
# Ignore stg schemas
12+
stg-schemas/

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.PHONY: help
2+
3+
.DEFAULT_GOAL := help
4+
5+
# ENVIRONMENT?=dev
6+
7+
## generate openapi models
8+
generate-openapi:
9+
openapi-generator generate -i https://api.permit.io/v2/openapi.json -g java -o generated/ -c openapi-config.json
10+
11+
clean-openapi:
12+
rm -rf generated/
13+
14+
## generate open api models from json schema
15+
generate-jsonschema:
16+
openapi2jsonschema https://api.permit.io/v2/openapi.json -o schemas/
17+

README.md

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,154 @@
11
![Java.png](imgs/Java.png)
2-
# Permit.io client for Java
2+
# Java SDK for Permit.io
33

4-
Java client library for the Permit.io full-stack permissions platform.
4+
Java SDK for interacting with the Permit.io full-stack permissions platform.
55

6+
## Overview
7+
8+
This guide will walk you through the steps of installing the Permit.io Java SDK and integrating it into your code.
9+
10+
## Installation
11+
12+
For [Maven](https://maven.apache.org/) projects, use:
13+
14+
```xml
15+
<dependency>
16+
<groupId>io.permit</groupId>
17+
<artifactId>permit-sdk-java</artifactId>
18+
<version>2.0.0</version>
19+
</dependency>
20+
```
21+
22+
For [Gradle](https://gradle.org/) projects, configure `permit-sdk-java` as a dependency in your `build.gradle` file:
23+
24+
```groovy
25+
dependencies {
26+
// ...
27+
28+
implementation 'io.permit:permit-sdk-java:2.0.0'
29+
}
30+
```
31+
32+
## Usage
33+
34+
### Initializing the SDK
35+
36+
To init the SDK, you need to create a new Permit client with the API key you got from the Permit.io dashboard.
37+
38+
First we will create a new `PermitConfig` object so we can pass it to the Permit client.
39+
40+
Second, we will create a new `Permit` client with the `PermitConfig` object we created.
41+
42+
```java
43+
import io.permit.sdk.Permit;
44+
import io.permit.sdk.PermitConfig;
45+
46+
// This line initializes the SDK and connects your Java app
47+
// to the Permit.io PDP container you've set up in the previous step.
48+
Permit permit = new Permit(
49+
new PermitConfig.Builder("[YOUR_API_KEY]")
50+
// in production, you might need to change this url to fit your deployment
51+
.withPdpAddress("http://localhost:7766")
52+
// optionally, if you wish to get more debug messages to your log, set this to true
53+
.withDebugMode(false)
54+
.build()
55+
);
56+
```
57+
58+
### Checking permissions
59+
60+
To check permissions using our `permit.check()` method, you will have to create User and Resource models as input to the permission check.
61+
The models are located in ``
62+
63+
Follow the example below:
64+
65+
```java
66+
import io.permit.sdk.enforcement.Resource;
67+
import io.permit.sdk.enforcement.User;
68+
import io.permit.sdk.Permit;
69+
70+
boolean permitted = permit.check(
71+
// building the user object using User.fromString()
72+
// the user key (this is the unique identifier of the user in the permission system).
73+
User.fromString("[USER KEY]"),
74+
// the action key (string)
75+
"create",
76+
// the resource object, can be initialized from string if the "default" tenant is used.
77+
Resource.fromString("document")
78+
);
79+
80+
if (permitted) {
81+
System.out.println("User is PERMITTED to create a document in the 'default' tenant");
82+
} else {
83+
System.out.println("User is NOT PERMITTED to create a document in the 'default' tenant");
84+
}
85+
```
86+
87+
A more complicated example (passing attributes on the user object, using an explicit tenant in the resource):
88+
89+
```java
90+
import io.permit.sdk.enforcement.Resource;
91+
import io.permit.sdk.enforcement.User;
92+
import java.util.HashMap;
93+
94+
95+
HashMap<String, Object> userAttributes = new HashMap<>();
96+
userAttributes.put("age", Integer.valueOf(20));
97+
userAttributes.put("favorite_color", "yellow");
98+
99+
boolean permitted = permit.check(
100+
// building the user object using the User.Builder class
101+
new User.Builder("[USER KEY]").withAttributes(userAttributes).build(),
102+
// the action key (string)
103+
"create",
104+
// building the resource object using the Resource.Builder in order to pass an explicit tenant key: "awesome-inc"
105+
new Resource.Builder("document").withTenant("awesome-inc").build()
106+
);
107+
108+
if (permitted) {
109+
System.out.println("User is PERMITTED to create a document in the 'awesome-inc' tenant");
110+
} else {
111+
System.out.println("User is NOT PERMITTED to create a document in the 'awesome-inc' tenant");
112+
}
113+
```
114+
115+
### Syncing users
116+
117+
When the user first logins, and after you check if he authenticated successfully (i.e: **by checking the JWT access token**) -
118+
you need to declare the user in the permission system so you can run `permit.check()` on that user.
119+
120+
To declare (or "sync") a user in the Permit.io API, use the `permit.api.users.sync()` method.
121+
122+
Follow the example below:
123+
124+
```java
125+
import io.permit.sdk.api.models.CreateOrUpdateResult;
126+
import io.permit.sdk.enforcement.User;
127+
128+
HashMap<String, Object> userAttributes = new HashMap<>();
129+
userAttributes.put("age", Integer.valueOf(50));
130+
userAttributes.put("fav_color", "red");
131+
132+
CreateOrUpdateResult<UserRead> result = permit.api.users.sync(
133+
(new User.Builder("auth0|elon"))
134+
.withEmail("elonmusk@tesla.com")
135+
.withFirstName("Elon")
136+
.withLastName("Musk")
137+
.withAttributes(userAttributes)
138+
.build()
139+
);
140+
UserRead user = result.getResult();
141+
assertTrue(result.wasCreated());
142+
```
143+
144+
Most params to UserCreates are optional, and only the unique user key is needed. This is valid:
145+
146+
```java
147+
CreateOrUpdateResult<UserRead> result = permit.api.users.sync(new UserCreate("[USER KEY]"));
148+
```
149+
150+
## Javadoc reference
151+
152+
To view the javadoc reference, [click here](https://javadoc.io/doc/io.permit/permit-sdk-java/2.0.0/index.html).
153+
154+
It's easiest to start with the root [Permit](https://javadoc.io/static/io.permit/permit-sdk-java/2.0.0/io/permit/sdk/Permit.html) class.

build.gradle

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ plugins {
1717
id 'com.palantir.git-version' version '0.13.0'
1818
// auto release to maven central (skip sonatype manual nexus release process)
1919
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
20+
// translate json schemas to java classes
21+
// id "org.jsonschema2pojo" version "1.1.3"
2022
}
2123

2224
// It is important to set the group and the version to the root project
@@ -33,6 +35,9 @@ repositories {
3335
}
3436

3537
java {
38+
toolchain {
39+
languageVersion = JavaLanguageVersion.of(8)
40+
}
3641
// sources are required by maven central in order to accept the package
3742
withSourcesJar()
3843
// javadoc jar is required by maven central in order to accept the package
@@ -41,22 +46,109 @@ java {
4146

4247
// package dependencies
4348
dependencies {
44-
implementation 'ch.qos.logback:logback-classic:1.2.10'
45-
implementation 'ch.qos.logback:logback-core:1.2.10'
49+
// swagger
50+
implementation 'io.swagger:swagger-annotations:1.6.5'
51+
52+
// http client
53+
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
54+
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.3'
55+
56+
// json serialization and deserialization
57+
implementation 'com.google.code.gson:gson:2.9.0'
58+
implementation 'io.gsonfire:gson-fire:1.8.5'
59+
60+
// openapi annotations
61+
implementation 'javax.ws.rs:jsr311-api:1.1.1'
62+
implementation 'javax.ws.rs:javax.ws.rs-api:2.1.1'
63+
implementation 'org.openapitools:jackson-databind-nullable:0.2.3'
64+
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
65+
implementation "jakarta.annotation:jakarta.annotation-api:1.3.5"
66+
67+
// logger
68+
implementation 'ch.qos.logback:logback-classic:1.4.12'
69+
implementation 'ch.qos.logback:logback-core:1.4.12'
4670
implementation 'org.slf4j:slf4j-api:1.7.33'
4771

72+
73+
4874
// Use JUnit Jupiter for testing.
4975
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'
5076

5177
// These dependencies are used internally, and not exposed to consumers on their own compile classpath.
5278
// google standard java library
5379
implementation 'com.google.guava:guava:30.1.1-jre'
54-
// json serialization and deserialization
55-
implementation 'com.google.code.gson:gson:2.8.9'
56-
// http client
57-
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
80+
81+
5882
}
5983

84+
//jsonSchema2Pojo {
85+
// // Location of the JSON Schema file(s). This may refer to a single file or a directory of files.
86+
// source = files("schemas/")
87+
//
88+
// // Target directory for generated Java source files. The plugin will add this directory to the
89+
// // java source set so the compiler will find and compile the newly generated source files.
90+
// targetDirectory = file("src/main/java")
91+
//
92+
// // Package name used for generated Java classes (for types where a fully qualified name has not
93+
// // been supplied in the schema using the 'javaType' property).
94+
// targetPackage = 'io.permit.sdk.openapi.models'
95+
//
96+
// // Whether to allow 'additional' properties to be supported in classes by adding a map to
97+
// // hold these. This is true by default, meaning that the schema rule 'additionalProperties'
98+
// // controls whether the map is added. Set this to false to globally disable additional properties.
99+
// includeAdditionalProperties = false
100+
//
101+
// // Whether to include a javax.annotation.Generated (Java 8 and lower) or
102+
// // javax.annotation.processing.Generated (Java 9+) in on generated types (default true).
103+
// // See also: targetVersion.
104+
// includeGeneratedAnnotation = true
105+
//
106+
// // Whether to use the 'title' property of the schema to decide the class name (if not
107+
// // set to true, the filename and property names are used).
108+
// useTitleAsClassname = true
109+
//
110+
// // Whether to empty the target directory before generation occurs, to clear out all source files
111+
// // that have been generated previously. <strong>Be warned</strong>, when activated this option
112+
// // will cause jsonschema2pojo to <strong>indiscriminately delete the entire contents of the target
113+
// // directory (all files and folders)</strong> before it begins generating sources.
114+
// removeOldOutput = false
115+
//
116+
// // Whether to generate builder-style methods of the form withXxx(value) (that return this),
117+
// // alongside the standard, void-return setters.
118+
// generateBuilders = true
119+
//
120+
// // If set to true, then the gang of four builder pattern will be used to generate builders on
121+
// // generated classes. Note: This property works in collaboration with generateBuilders.
122+
// // If generateBuilders is false then this property will not do anything.
123+
// useInnerClassBuilders = false
124+
//
125+
// // Whether to include hashCode and equals methods in generated Java types.
126+
// includeHashcodeAndEquals = false
127+
//
128+
// // Whether to include a toString method in generated Java types.
129+
// includeToString = false
130+
//
131+
// // Whether to include getters or to omit these accessor methods and create public fields instead.
132+
// includeGetters = false
133+
//
134+
// // Whether to include setters or to omit these accessor methods and create public fields instead.
135+
// includeSetters = false
136+
//
137+
// // Whether to use java.util.Optional for getters on properties that are not required
138+
// useOptionalForGetters = true
139+
//
140+
// // Whether to generate constructors or not.
141+
// includeConstructors = true
142+
//
143+
// // Whether to include only 'required' fields in generated constructors
144+
// constructorsRequiredPropertiesOnly = true
145+
//
146+
// annotationStyle = 'gson'
147+
//
148+
// // Whether to initialize Set and List fields as empty collections, or leave them as null.
149+
// initializeCollections = false
150+
//}
151+
60152
publishing {
61153
repositories {
62154
maven {

openapi-config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"apiPackage": "io.permit.sdk.openapi.api",
3+
"invokerPackage": "io.permit.sdk.openapi.client",
4+
"modelPackage": "io.permit.sdk.openapi.model",
5+
"serializationLibrary": "gson",
6+
"legacyDiscriminatorBehavior": "true"
7+
}

schemas/actionblockeditable.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"title": "ActionBlockEditable",
3+
"type": "object",
4+
"properties": {
5+
"name": {
6+
"title": "Name",
7+
"type": "string",
8+
"description": "a more descriptive name for the action"
9+
},
10+
"description": {
11+
"title": "Description",
12+
"type": "string",
13+
"description": "optional description string explaining what this action represents in your system"
14+
},
15+
"attributes": {
16+
"title": "Attributes",
17+
"type": "object",
18+
"description": "Arbitrary action attributes that can be used for filtering or enforcement of attribute-based access control policies.",
19+
"default": {},
20+
"existingJavaType": "java.util.HashMap<String, Object>"
21+
}
22+
},
23+
"additionalProperties": false,
24+
"$schema": "http://json-schema.org/schema#"
25+
}

0 commit comments

Comments
 (0)