box / box-windows-sdk-v2

Windows SDK for v2 of the Box API. The SDK is built upon .NET Framework 4.5
https://developer.box.com
Apache License 2.0
186 stars 163 forks source link

NullReferenceException when calling BoxJWTAuth.UserToken() #645

Closed JoshHawley closed 3 years ago

JoshHawley commented 4 years ago

Description of the Issue

I am new to this API, and just figuring out how to use it. I have read quite a bit of documentation, and it is possible that some of my assumptions are wrong. However, I felt the need to report this in any case because I thought that you would want to know that the actual error is being hidden and not returned from the API.

I originally found this error when using the Nuget package, and checked out the most current code from this repo to investigate. I would have submitted a pull request to fix it, but decided that my lack of knowledge about this API means that I don't understand the exception handling strategy.

When attempting to get a user token, a NullReferenceException is thrown from a catch block that obfuscates the intended exception.

This is from Box.V2\JWTAuth\BoxJWTAuth.cs just before it throws: image

ex.Error.Code is null and that is throwing from near the end of the if conditional section.

Versions Used

.NET SDK: Nuget package Box.V2 3.22.0 AND cloned from this repo using commit e2c679db7a21431b379dfb39cd8a590f28d95e3a (same issue) .NET Framework/Core: Framework 4.7.2

Steps to Reproduce

(This is a guess at how to reproduce based on my situation. I cannot test due to not being an enterprise admin)

  1. Create application in box account that is admin approved, but does not have "Perform actions as users" enabled. (I know that I need this for this to work, I'm reporting a bug in the error handling)
  2. Authenticate with BoxJWTAuth
  3. Execute code: var boxConfig = new BoxConfig(CLIENT_ID, CLIENT_SECRET, ENTERPRISE_ID, BOX_PRIVATE_KEY, JWT_PRIVATE_KEY_PASSWORD, JWT_PUBLIC_KEY_ID); var boxJWT = new BoxJWTAuth(boxConfig); var adminToken = boxJWT.AdminToken(); var adminClient = boxJWT.AdminClient(adminToken); var userToken = boxJWT.UserToken("a valid userid from your enterprise");

Error Message, Including Stack Trace

$exception | {"Object reference not set to an instance of an object."} | System.NullReferenceException

at Box.V2.JWTAuth.BoxJWTAuth.GetToken(String subType, String subId) in C:_git\box-windows-sdk-v2\Box.V2\JWTAuth\BoxJWTAuth.cs:line 173 (actually 185 because the if conditional is large)

sujaygarlanka commented 4 years ago

@JoshHawley Thank you for the feedback! We will take a look at this and get back to you.

DaleyKD commented 4 years ago

@JoshHawley - Just to double-check (I was getting something similar to this today)... did you Authorize your app in the Box Developer Console?

baskarmib commented 4 years ago

Facing similar issue while calling session.UserToken(validUserId) to create UserClient. I am not facing any issues with the AdminToken and AdminClient. This is only after the upgrade to latest nuget packages.

In my case I created new public and private key, Do I need to resubmit the custom app for Authorization? If that is the case how the AdminToken and AdminClient calls are handled?

DaleyKD commented 4 years ago

@baskarmib - In my previous experience (which is limited, mind you), any time you did any kind of tweaks, you had to resubmit the application for auth. I don't know how to answer the rest of your questions, though.

baskarmib commented 4 years ago

@DaleyKD I did reauthorize my app but no luck. It looks like the app does not allow to create User token for an App User which is created with IsPlatform as true. The same code snippets used to work earlier but not now. I now only have to delete the entire app and recreate using new set of keys and complete the authorization.

mkiosugadaikichi commented 4 years ago

I reproduced by following steps

  1. set break point at line 161.
  2. when break point hits, wait enough time in order to make assertion expired.
  3. resume execution.

line 172 comment tells about assertion expiration. I found ex.Error.Code is null.

PJSimon commented 3 years ago

Closing this issue as the fix for this issue (#697) will be in the next release, which should deploy in the next week or two.

matsukawar commented 3 years ago

I faced same situation so I rollbacked the module 3.24.0 to 3.18.0 and worked well.