Skip to content

Commit

Permalink
Merge pull request #307 from GDATASoftwareAG/Typescript/ResourceOwner…
Browse files Browse the repository at this point in the history
…PasswordGrantAuthenticator

Typescript/resource owner password grant authenticator
  • Loading branch information
lennartdohmann authored Oct 10, 2023
2 parents b7d0606 + 58954f5 commit 6643cea
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci-java.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ jobs:
gradle-version: "8.1"
build-root-directory: java/examples/VaasExample

- name: run authentication examples
uses: gradle/gradle-build-action@v2
with:
arguments: authentication
gradle-version: "8.1"
build-root-directory: java/examples/VaasExample

- name: extract version
if: startsWith(github.ref, 'refs/tags/java')
run: |
Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/ci-typescript.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,17 @@ jobs:
- name: run examples scan file
env:
SCAN_PATH: "main.ts"
run: npx ts-node main.ts
run: pnpm start:filescan
working-directory: typescript/examples/VaasExample

- name: run examples authentication
env:
SCAN_PATH: "authentication.ts"
run: pnpm start:authentication
working-directory: typescript/examples/VaasExample

- name: run examples scan url
run: npx ts-node url_scan.ts
run: pnpm start:urlscan
working-directory: typescript/examples/VaasExample

- name: extract version
Expand Down
5 changes: 5 additions & 0 deletions java/examples/VaasExample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ task urlScan(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
mainClass = 'de.gdata.vaasexample.UrlScan'
}

task authentication(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
mainClass = 'de.gdata.vaasexample.Authentication'
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package de.gdata.vaasexample;

import de.gdata.vaas.ClientCredentialsGrantAuthenticator;
import de.gdata.vaas.ResourceOwnerPasswordGrantAuthenticator;
import de.gdata.vaas.Vaas;
import de.gdata.vaas.VaasConfig;
import de.gdata.vaas.*;
import de.gdata.vaas.messages.VerdictRequestAttributes;
import java.net.URI;
import java.nio.file.Path;


public class Authentication {
Expand All @@ -15,7 +11,6 @@ public static void main(String[] args) throws Exception {
var clientSecret = System.getenv("CLIENT_SECRET");
var userName = System.getenv("VAAS_USER_NAME");
var password = System.getenv("VAAS_PASSWORD");
var scanPath = System.getenv("SCAN_PATH");
var tokenUrl = System.getenv("TOKEN_URL");
if (tokenUrl == null) { tokenUrl = "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"; }
var vaasUrl = System.getenv("VAAS_URL");
Expand All @@ -42,10 +37,9 @@ public static void main(String[] args) throws Exception {
var vaas = new Vaas(config, authenticator);
vaas.connect();

var file = Path.of(scanPath);
var verdictRequestAttributes = new VerdictRequestAttributes();
verdictRequestAttributes.setTenantId("fileTenant");
var verdict = vaas.forFile(file, verdictRequestAttributes);
var verdict = vaas.forSha256(new Sha256("275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f"), verdictRequestAttributes);
vaas.disconnect();
System.out.printf("File %s was detected as %s", verdict.getSha256(), verdict.getVerdict());
}
Expand Down
21 changes: 18 additions & 3 deletions typescript/examples/VaasExample/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { promises as fs } from "fs";
import {
ClientCredentialsGrantAuthenticator,
ResourceOwnerPasswordGrantAuthenticator,
Vaas,
} from "gdata-vaas";

Expand All @@ -18,16 +19,30 @@ function getFromEnvironment(key: string) {
async function main() {
const CLIENT_ID = getFromEnvironment("CLIENT_ID");
const CLIENT_SECRET = getFromEnvironment("CLIENT_SECRET");
const VAAS_USER_NAME = getFromEnvironment("VAAS_USER_NAME");
const VAAS_PASSWORD = getFromEnvironment("VAAS_PASSWORD");
const SCAN_PATH = getFromEnvironment("SCAN_PATH");
const TOKEN_URL = getFromEnvironment("TOKEN_URL");
const VAAS_URL = getFromEnvironment("VAAS_URL");

const authenticator = new ClientCredentialsGrantAuthenticator(
CLIENT_ID,
CLIENT_SECRET,
// If you got a username and password from us, you can use the ResourceOwnerPasswordAuthenticator like this
const authenticator = new ResourceOwnerPasswordGrantAuthenticator(
"vaas-customer",
VAAS_USER_NAME,
VAAS_PASSWORD,
TOKEN_URL
);

// You may use self registration and create a new username and password for the
// ResourceOwnerPasswordAuthenticator by yourself like the example above on https://vaas.gdata.de/login

// Else if you got a client id and client secret from us, you can use the ClientCredentialsGrantAuthenticator like this
// const authenticator = new ClientCredentialsGrantAuthenticator(
// CLIENT_ID,
// CLIENT_SECRET,
// TOKEN_URL
// );

const vaas = new Vaas();
const token = await authenticator.getToken()
await vaas.connect(token, VAAS_URL);
Expand Down
5 changes: 3 additions & 2 deletions typescript/examples/VaasExample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"scripts": {
"start:filescan": "npx ts-node main.ts",
"start:urlscan": "npx ts-node url_scan.ts",
"start:authentication": "npx ts-node authentication.ts",
"build": "tsc",
"watch": "tsc -w"
},
"author": "",
"license": "ISC",
"dependencies": {
"gdata-vaas": "5.3.0",
"gdata-vaas": "6.0.0",
"ts-node": "^10.9.1",
"@types/node": "20.4.8",
"typescript": "5.1.6"
Expand All @@ -20,4 +21,4 @@
"dotenv": "^16.0.0",
"ts-node": "^10.9.1"
}
}
}
8 changes: 4 additions & 4 deletions typescript/examples/VaasExample/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6643cea

Please sign in to comment.