Skip to content

Create a Box application with JWT auth

Anusha Ranganathan edited this page Aug 22, 2018 · 8 revisions

JWT application setup

https://developer.box.com/docs/setting-up-a-jwt-app

  1. Login as archivematica user using the university of hull box account
  2. Click on Dev Console in the left hand menu and go to the Box developer console (link)
  3. click on the Create New App option.
  4. On the next page, click on the type of application you'd like to build (Custom App) and click the Next button.
  5. On the authentication method page that comes up, click on the option for OAuth 2.0 with JWT and click the Next button.
  6. Give your application a unique name (AutoArchiver), click the Create App button, then click the View Your App button on the next page.
  7. Your newly created JWT application (app link) will be presented on the Configuration page, allowing you to further configure it. To do so, adjust these settings:
    • Access:
      • Application Access
    • Scopes:
      • Read all files and folders stored in Box
      • Read and write all files and folders stored in Box
      • Manage users
      • Manage groups
      • Manage webhooks
    • Advanced Features
      • Perform actions as users will allow your application to make API requests on behalf of a user, instead of the app itself.
      • Generate user access tokens will allow you to access APIs without the user having to log in with their credentials.
    • Generate a Public / Private Keypair
      • Click on the button generate a public or private key pair
      • Save the box config.json file (611889_t7g8sady_config.json)
    • Request the system administrators to grant access to the app (this step needs to be done everytime the scope of the app is modified)

To test the app using the developer token (valid for 60 mins)

curl https://api.box.com/2.0/folders/0 -H "Authorization: Bearer 3kkB4NijFP3Wkzl5SK1ZuwNk2gnWz2v0"

Authenticate with JWT (SDK)

https://developer.box.com/docs/authenticate-with-jwt

  1. Save the private key from the json config file in a separate file, so it's easy to read. Parse the json config file in the command line
    require 'json'
    json_config = File.read('config/611889_907insbl_config.json')
    box_config = JSON.parse(json_config)
    f = File.open('AutoArchiver.pem', 'w')
    f.write(box_config['boxAppSettings']['appAuth']['privateKey']) 
    f.close
    
  2. Install the SDK Add the following to your gemfile
    gem 'boxr'
    
    Then execute the following from a terminal / command prompt
    $ bundle install
    
  3. Test the app authenticates from the rails console
    bundle exec rails console
    
    Test generating tokens for the enterprise and user accounts using the values from the saved config file
    require 'boxr'
    tokens = Boxr::get_enterprise_token(
      enterprise_id: ENTERPRISE_ID,
      private_key: File.read(JWT_SECRET_KEY_PATH),
      private_key_password: JWT_SECRET_KEY_PASSWORD,
      public_key_id: PUBLIC_KEY_ID,
      client_id: CLIENT_ID,
      client_secret: CLIENT_SECRET
    )
    # Boxr::BoxrError: 400: {"error":"unauthorized_client","error_description":"This app is not authorized by the enterprise admin"}
    
    tokens = Boxr::get_user_token(
      USER_ID, 
      private_key: File.read(JWT_SECRET_KEY_PATH),
      private_key_password: JWT_SECRET_KEY_PASSWORD,
      public_key_id: PUBLIC_KEY_ID,
      client_id: CLIENT_ID, 
      client_secret: CLIENT_SECRET
    )
    # Boxr::BoxrError: 400: {"error":"unauthorized_client","error_description":"This app is not authorized by the enterprise admin"}
    

Reference for Boxr Client:

Authenticate with JWT (API)

https://developer.box.com/docs/construct-jwt-claim-manually

  1. Construct the JWT assertion using boxr and the values from the config file
    require 'boxr'
    require 'openssl'
    private_key = OpenSSL::PKey::RSA.new(File.read(JWT_SECRET_KEY_PATH), JWT_SECRET_KEY_PASSWORD)
    assertion = Boxr::jwt_assertion(private_key, CLIENT_ID, USER_ID, 'user', PUBLIC_KEY_ID)
    puts assertion
    
  2. Submit a HTTP POST to generate JWT token
    Request
    Endpoint
        https://api.box.com/oauth2/token      
    HEADERS
        Content-Type: application/x-www-form-urlencoded
    BODY (x-www-form-urlencoded)
        grant_type:    urn:ietf:params:oauth:grant-type:jwt-bearer
        client_id:     CLIENT_ID 
        client_secret: CLIENT_SECRET
        assertion:     assertion
    
    Response
    400 Bad Request
    {
    "error": "unauthorized_client",
    "error_description": "This app is not authorized by the enterprise admin"
    }
    

Work with files

https://developer.box.com/docs/work-with-files

Work with users

https://developer.box.com/docs/work-with-users

Work with Webhooks

https://developer.box.com/docs/work-with-webhooks