Javascript Adapter (csw-aas-js)

csw-aas-js is an npm package which provides React components that integrate with the CSW Authentication and Authorization Service. UI applications can use these React components to enable the application to show or hide components based on the authentication and authorization policy. csw-aas-js is written in typescript and it bundles transpiled es6 module along with type declarations.

Dependencies

To use the csw-aas-js adapter, run this command from root folder of your application where package.json exists:

npm
    npm i --save csw-aas-js@0.1-SNAPSHOT
yarn
    yarn add csw-aas-js@0.1-SNAPSHOT

Application Configuration

Web application needs following configurations in order to get access token from keycloak server. This application specific config object should be passed in AuthContextProvider component. There are two configurations needed for a web application i.e. realm, clientId

realm is a mandatory configuration which specified in keycloak server under which client for your application is registered.

clientId is a mandatory configuration which specifies the client id of the app as per registration in keycloak server.

Javascript
export const AppConfig = {
  realm: 'example',
  clientId: 'example-app',
}

Components

csw-aas-js exposes the following React components.

Components can be imported as shown in code snippet below

Javascript
import { AuthContext, Logout, Login } from 'csw-aas-js'

AuthContextProvider

AuthContextProvider is wrapper over a React Context.Provider. A JSON configuration file must be passed in that contains the application specific AAS server configuration (e.g. clientId, realm). When a user logs in, an AAS Server is instantiated, with the UI application specific configuration overriding the predefined configuration. Once the AAS sever is instantiated, an auth object is created with the needed attributes and APIs. This auth object is available to other React components; since AuthContextProvider is a Provider, its data can be shared with any of the children React components in its tree in a Consumer component (see below). All Consumers that are descendants of a Provider will re-render whenever the AuthContextProvider’s state changes, e.g. a user authorizes. It is recommended to use AuthContextProvider to wrap the entire application so that data can be shared anywhere in application via a Consumer.

Javascript
<AuthContextProvider config={AppConfig}>
  <BrowserRouter>
    <div>
      <NavComponent />
      <Route
        exact
        path='/secured'
        render={_ => (
          <CheckLogin error={<ExampleError />}>
            <Write />
          </CheckLogin>
        )}
      />
      <Route exact path='/public' component={Read} />
    </div>
  </BrowserRouter>

  {
    <RealmRole realmRole='example-admin-role' error={<ExampleError />}>
      <div>Example admin role specific functionality</div>
    </RealmRole>
  }
  {
    <ClientRole
      clientRole='person-role'
      client='example-server'
      error={<ExampleError />}>
      <div>Person role specific functionality</div>
    </ClientRole>
  }
</AuthContextProvider>

Source code for RealmRole component

Consumer

Consumer is similar to a React Context.Consumer. The shared auth object from the AuthContextProvider can be accessed using a Consumer component

Javascript
const { auth } = useContext(AuthContext)
return (
  <div className='nav-wrapper'>
    {auth && auth.isAuthenticated() ? (
      <div>
        Hello, you are logged in
        <div>Open functionality</div>
      </div>
    ) : (
      <div>
        Hello, you are not logged in
        <div>Open functionality</div>
      </div>
    )}
  </div>
)

Source code for RealmRole component

Login

The Login component instantiates an AAS server with the configurations provided. It redirects to an AAS server login page for the user to login. After login, the auth object in the context is updated with the appropriate values, e.g. token, realm & client roles etc.

Javascript
<Login />

Source code for Login component

Logout

The Logout component logs out the user from the AAS server. It clears the auth object stored in the context.

Javascript
  <Logout />
) : (

Source code for Logout component

CheckLogin

CheckLogin components provide ability to show something only if the user is logged in. In the following code snippet, Write is a react component that is shown only if the user is logged in. The behavior if the user is not logged in can be defined by an HTML element or React component that is passed into the component as an error property, shown as an ExampleError Component in following snippet.

Javascript
<CheckLogin error={<ExampleError />}>
  <Write />
</CheckLogin>

Source code for CheckLogin component

RealmRole

RealmRole components provide the ability to show something only if the user is logged in and has the specified realm role. In the following code snippet, the contents of the div block are shown only if the user is logged in and has the realm role specified in the realmRole prop. Similar to CheckLogin, the behaviour if the user is not logged in can be optionally defined by an HTML element or React component that is passed into the component as an error property, shown as an ExampleError Component in following snippet.

Javascript
<RealmRole realmRole='example-admin-role' error={<ExampleError />}>
  <div>Example admin role specific functionality</div>
</RealmRole>

Source code for RealmRole component

ClientRole

ClientRole components provide the ability to show something only if the user is logged in and has the specified client role for the specified client. In the following code snippet, the contents of the div block are shown only if the user is logged in and has the client role for specified client in the clientRole prop. Similar to RealmRole, the behaviour if the user is not logged in can be optionally defined by an HTML element or React component that is passed into the component as an error property, shown as an ExampleError Component in following snippet.

Javascript
<ClientRole
  clientRole='person-role'
  client='example-server'
  error={<ExampleError />}>
  <div>Person role specific functionality</div>
</ClientRole>

Source code for ClientRole component

Technical Description

See csw-aas-js Technical Description.