/// <summary>
        /// Constructor for JWT authentication
        /// </summary>
        /// <param name="boxConfig">Config contains information about client id, client secret, enterprise id, private key, private key password, public key id </param>
        public BoxJWTAuth(IBoxConfig boxConfig)
        {
            this.boxConfig = boxConfig;

            // the following allows creation of a BoxJWTAuth object without valid keys but with a valid JWT UserToken
            // this allows code like this:

            // var boxConfig = new BoxConfig("", "", "", "", "", "");
            // var boxJwt = new BoxJWTAuth(boxConfig);
            // const string userToken = "TOKEN_OBTAINED_BY_CALLING_FULL_BOXJWTAUTH";  // token valid for 1 hr.
            // UserClient = boxJwt.UserClient(userToken, null);  // this user client can do normal file operations.

            if (!string.IsNullOrEmpty(boxConfig.JWTPrivateKey) && !string.IsNullOrEmpty(boxConfig.JWTPrivateKeyPassword))
            {
                var    pwf = new PEMPasswordFinder(this.boxConfig.JWTPrivateKeyPassword);
                object key = null;
                using (var reader = new StringReader(this.boxConfig.JWTPrivateKey))
                {
                    var privateKey = new PemReader(reader, pwf).ReadObject();

                    key = privateKey;
                }

                if (key == null)
                {
                    throw new BoxException("Invalid private key!");
                }

                RSA rsa = null;
                if (key is AsymmetricCipherKeyPair)
                {
                    var ackp = (AsymmetricCipherKeyPair)key;
                    rsa = DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)ackp.Private);
                }
                else if (key is RsaPrivateCrtKeyParameters)
                {
                    var rpcp = (RsaPrivateCrtKeyParameters)key;
                    rsa = DotNetUtilities.ToRSA(rpcp);
                }

#if NETSTANDARD1_4
                this.credentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256);
#else
                this.credentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256Signature, SecurityAlgorithms.Sha256Digest);
#endif
            }
        }
Пример #2
0
        /// <summary>
        /// Constructor for JWT authentication
        /// </summary>
        /// <param name="boxConfig">Config contains information about client id, client secret, enterprise id, private key, private key password, public key id </param>
        public BoxJWTAuth(IBoxConfig boxConfig)
        {
            this.boxConfig = boxConfig;

            var pwf = new PEMPasswordFinder(this.boxConfig.JWTPrivateKeyPassword);
            AsymmetricCipherKeyPair key;

            using (var reader = new StringReader(this.boxConfig.JWTPrivateKey))
            {
                key = (AsymmetricCipherKeyPair) new PemReader(reader, pwf).ReadObject();
            }
            var rsa = DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)key.Private);

#if NETSTANDARD1_4
            this.credentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256);
#else
            this.credentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256Signature, SecurityAlgorithms.Sha256Digest);
#endif
        }