Skip to content

Commit

Permalink
Added Catalan and Japanes language to translation.md. Added Badge for…
Browse files Browse the repository at this point in the history
… translation status

Signed-off-by: Robin Meese <39960884+robson90@users.noreply.github.com>
  • Loading branch information
jonkoops authored and robson90 committed Oct 18, 2024
1 parent 7bdd10d commit 9da1117
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 31 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[![Artifact Hub](https://img.shields.io/endpoint?url=https://artifacthub.io/badge/repository/keycloak-operator)](https://artifacthub.io/packages/olm/community-operators/keycloak-operator)
![GitHub Repo stars](https://img.shields.io/github/stars/keycloak/keycloak?style=flat)
![GitHub commit activity](https://img.shields.io/github/commit-activity/m/keycloak/keycloak)
[![Translation status](https://hosted.weblate.org/widget/keycloak/svg-badge.svg)](https://hosted.weblate.org/engage/keycloak/)


# Open Source Identity and Access Management
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const keycloak = new Keycloak({
});
const authorization = new KeycloakAuthorization(keycloak);
await keycloak.init();
// Now you can use the authorization object to interact with the server.
----

The `keycloak-js/authz` library provides two main features:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ Starting from this release, when `--cache-config-file` is not set, the default I
The embedded `work` cache needs to be configured as a `replicated-cache` for cache invalidation to work as expected.

Starting from this release, {project_name} check this at startup and will fail to start if it is not configured as such.

= Deprecated APIs for JavaScript Authorization client

The following APIs for the JavaScript Authorization client are deprecated and will be removed in the next major release:

- The `ready` property on the `KeycloakAuthorization` instance.
- The `init()` method on the `KeycloakAuthorization` instance.

These APIs are no longer needed as initialization is done automatically on demand when calling methods on the `KeycloakAuthorization` instance. You can safely remove any code that depends on these APIs.
8 changes: 7 additions & 1 deletion docs/translation.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ While were in the transitioning process towards Weblate, the following translati
- German
- Dutch
- French
- Catalan
- Japanese

For all other translations, look for the `messages_*.properties` files in the main repository and create a pull request with your translations.

Expand All @@ -52,7 +54,8 @@ If you have any questions or need assistance, feel free to reach out to the lang
* German: [Robin Meese](https://github.com/robson90)
* Dutch: [Jon Koops](https://github.com/jonkoops)
* French: [Dimitri](https://github.com/DimitriDR) && [Gilbert Fernandes](https://github.com/gilbert-fernandes)

* Catalan: [jmallach](https://github.com/jmallach) && [Ecron](https://github.com/Ecron)
* Japanese: [y-tabata](https://github.com/y-tabata) && [wadahiro](https://github.com/wadahiro)

### Steps to Add a New Language

Expand All @@ -64,3 +67,6 @@ If you have any questions or need assistance, feel free to reach out to the lang
- invite the two volunteers
- comment on discussion thread, that the langauge has been enabled
- Weblate synchronizes daily, so it may take up to 24 hours after enabling the language before you can start your first translations

### Translation status
[![Translation status](https://hosted.weblate.org/widget/keycloak/multi-auto.svg)](https://hosted.weblate.org/engage/keycloak/)
10 changes: 10 additions & 0 deletions js/libs/keycloak-js/lib/keycloak-authz.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,18 @@ declare class KeycloakAuthorization {
rpt: any;
config: { rpt_endpoint: string };

/**
* Initializes the `KeycloakAuthorization` instance.
* @deprecated Initialization now happens automatically, calling this method is no longer required.
*/
init(): void;

/**
* A promise that resolves when the `KeycloakAuthorization` instance is initialized.
* @deprecated Initialization now happens automatically, using this property is no longer required.
*/
ready: Promise<void>;

/**
* This method enables client applications to better integrate with resource servers protected by a Keycloak
* policy enforcer using UMA protocol.
Expand Down
135 changes: 105 additions & 30 deletions js/libs/keycloak-js/lib/keycloak-authz.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,45 @@ var KeycloakAuthorization = function (keycloak, options) {
var _instance = this;
this.rpt = null;

var resolve = function () {};
var reject = function () {};

// detects if browser supports promises
if (typeof Promise !== "undefined" && Promise.toString().indexOf("[native code]") !== -1) {
this.ready = new Promise(function (res, rej) {
resolve = res;
reject = rej;
});
}
// Only here for backwards compatibility, as the configuration is now loaded on demand.
// See:
// - https://github.com/keycloak/keycloak/pull/6619
// - https://issues.redhat.com/browse/KEYCLOAK-10894
// TODO: Remove both `ready` property and `init` method in a future version
Object.defineProperty(this, 'ready', {
get() {
console.warn("The 'ready' property is deprecated and will be removed in a future version. Initialization now happens automatically, using this property is no longer required.");
return Promise.resolve();
},
});

this.init = () => {
console.warn("The 'init()' method is deprecated and will be removed in a future version. Initialization now happens automatically, calling this method is no longer required.");
};

this.init = function () {
var request = new XMLHttpRequest();

request.open('GET', keycloak.authServerUrl + '/realms/' + keycloak.realm + '/.well-known/uma2-configuration');
request.onreadystatechange = function () {
if (request.readyState == 4) {
if (request.status == 200) {
_instance.config = JSON.parse(request.responseText);
resolve();
} else {
console.error('Could not obtain configuration from server.');
reject();
}
}
/** @type {Promise<unknown> | undefined} */
let configPromise;

/**
* Initializes the configuration or re-uses the existing one if present.
* @returns {Promise<void>} A promise that resolves when the configuration is loaded.
*/
async function initializeConfigIfNeeded() {
if (_instance.config) {
return _instance.config;
}

request.send(null);
};
if (configPromise) {
return await configPromise;
}

if (!keycloak.didInitialize) {
throw new Error('The Keycloak instance has not been initialized yet.');
}

configPromise = loadConfig(keycloak.authServerUrl, keycloak.realm);
_instance.config = await configPromise;
}

/**
* This method enables client applications to better integrate with resource servers protected by a Keycloak
Expand All @@ -57,7 +67,14 @@ var KeycloakAuthorization = function (keycloak, options) {
* The authorization request must be provided with a ticket.
*/
this.authorize = function (authorizationRequest) {
this.then = function (onGrant, onDeny, onError) {
this.then = async function (onGrant, onDeny, onError) {
try {
await initializeConfigIfNeeded();
} catch (error) {
handleError(error, onError);
return;
}

if (authorizationRequest && authorizationRequest.ticket) {
var request = new XMLHttpRequest();

Expand Down Expand Up @@ -121,7 +138,14 @@ var KeycloakAuthorization = function (keycloak, options) {
* Obtains all entitlements from a Keycloak Server based on a given resourceServerId.
*/
this.entitlement = function (resourceServerId, authorizationRequest) {
this.then = function (onGrant, onDeny, onError) {
this.then = async function (onGrant, onDeny, onError) {
try {
await initializeConfigIfNeeded();
} catch (error) {
handleError(error, onError);
return;
}

var request = new XMLHttpRequest();

request.open('POST', _instance.config.token_endpoint, true);
Expand Down Expand Up @@ -213,9 +237,60 @@ var KeycloakAuthorization = function (keycloak, options) {
return this;
};

this.init(this);

return this;
};

/**
* Obtains the configuration from the server.
* @param {string} serverUrl The URL of the Keycloak server.
* @param {string} realm The realm name.
* @returns {Promise<unknown>} A promise that resolves when the configuration is loaded.
*/
async function loadConfig(serverUrl, realm) {
const url = `${serverUrl}/realms/${encodeURIComponent(realm)}/.well-known/uma2-configuration`;

try {
return await fetchJSON(url);
} catch (error) {
throw new Error('Could not obtain configuration from server.', { cause: error });
}
}

/**
* Fetches the JSON data from the given URL.
* @param {string} url The URL to fetch the data from.
* @returns {Promise<unknown>} A promise that resolves when the data is loaded.
*/
async function fetchJSON(url) {
let response;

try {
response = await fetch(url);
} catch (error) {
throw new Error('Server did not respond.', { cause: error });
}

if (!response.ok) {
throw new Error('Server responded with an invalid status.');
}

try {
return await response.json();
} catch (error) {
throw new Error('Server responded with invalid JSON.', { cause: error });
}
}

/**
* @param {unknown} error
* @param {((error: unknown) => void) | undefined} handler
*/
function handleError(error, handler) {
if (handler) {
handler(error);
} else {
console.error(message, error);
}
}

export default KeycloakAuthorization;

0 comments on commit 9da1117

Please sign in to comment.