Esempio n. 1
0
        private static void InitializeSample(Options opts)
        {
            /*
             * Using the Noark 5 web services requires providing a valid access token.
             * The way this token is obtained depends on the system implementing the services.
             * This sample code obtains the token from the Documaster's identity provider service
             * with the help of a designated Documaster IDP client.
             * If the Noark client is used in the context of an application that has access to a web browser,
             * we strongly recommend choosing the Oauth2 Authorization Code Grant Flow supported for obtaining
             * access tokens.
             */

            //Initialize an IDP client and request an authorization token
            InitIdpClient(opts);
            PasswordGrantTypeParams passwordGrantTypeParams = new PasswordGrantTypeParams(
                opts.ClientId, opts.ClientSecret, opts.Username, opts.Password, OpenIDConnectScope.OPENID);
            var accessToken = idpClient.GetTokenWithPasswordGrantType(passwordGrantTypeParams).AccessToken;

            //Initialize a Noark client
            InitClient(opts);
            client.AuthToken = accessToken;

            //Notice that it is also possible to initialize а ssl-based Noark client without providing
            //client certificate:
            //InitClientWithoutClientCertificate(opts);

            testDoc = opts.TestDoc;
        }
Esempio n. 2
0
        private void RefreshAccessToken()
        {
            //access token expires in 60 minutes

            if (this.refreshToken == null)
            {
                PasswordGrantTypeParams passwordGrantTypeParams = new PasswordGrantTypeParams(this.opts.ClientId,
                                                                                              this.opts.ClientSecret, this.opts.Username, this.opts.Password, OpenIDConnectScope.OPENID);
                AccessTokenResponse accessTokenResponse =
                    this.idpClient.GetTokenWithPasswordGrantType(passwordGrantTypeParams);
                this.accessTokenExpirationTime = DateTime.Now.AddSeconds(accessTokenResponse.ExpiresInMs);
                this.refreshToken          = accessTokenResponse.RefreshToken;
                this.noarkClient.AuthToken = accessTokenResponse.AccessToken;
            }
            else if (DateTime.Now > this.accessTokenExpirationTime.AddSeconds(-20))
            {
                RefreshTokenGrantTypeParams refreshTokenGrantTypeParams =
                    new RefreshTokenGrantTypeParams(this.refreshToken, this.opts.ClientId, this.opts.ClientSecret,
                                                    OpenIDConnectScope.OPENID);
                AccessTokenResponse accessTokenResponse = this.idpClient.RefreshToken(refreshTokenGrantTypeParams);
                this.accessTokenExpirationTime = DateTime.Now.AddSeconds(accessTokenResponse.ExpiresInMs);
                this.refreshToken          = accessTokenResponse.RefreshToken;
                this.noarkClient.AuthToken = accessTokenResponse.AccessToken;
            }
        }