public async Task <AccountConfiguration> CreateAccount_Oidc()
        {
            var f = new OidcWaitForm();

            f.InitEx(StorageType.HiDrive);
            f.Show();


            var browser = new OidcSystemBrowser(Enumerable.Range(50001, 50020));

            var redirectUri = browser.RedirectUri;

            var uri   = _authenticator.GetAuthorizationCodeRequestUrl(new AuthorizationScope(AuthorizationRole.User, AuthorizationPermission.ReadWrite), redirectUri);
            var query = await browser.GetQueryStringAsync(uri.ToString(), f.CancellationToken);

            var code  = query["code"];
            var token = await _authenticator.AuthenticateByAuthorizationCodeAsync(code);

            var client = HiDriveHelper.GetClient(_authenticator);
            var user   = await client.User.Me.Get().ExecuteAsync();

            var account = new AccountConfiguration()
            {
                Type   = StorageType.HiDrive,
                Id     = user.Account,
                Name   = user.Alias,
                Secret = _authenticator.Token.RefreshToken,
            };


            f.Close();

            return(account);
        }
Exemple #2
0
        //public async Task<AccountConfiguration> CreateAccount()
        //{
        //    var type = _isAccessRestricted ? StorageType.DropboxRestricted : StorageType.Dropbox;
        //    var clientId = _isAccessRestricted ? DropboxHelper.DropboxAppFolderOnlyClientId : DropboxHelper.DropboxFullAccessClientId;
        //    var clientSecret = _isAccessRestricted ? DropboxHelper.DropboxAppFolderOnlyClientSecret : DropboxHelper.DropboxFullAccessClientSecret;
        //    string[] scopes =
        //    {
        //        "account_info.read",
        //        "files.metadata.write",
        //        "files.metadata.read",
        //        "files.content.write",
        //        "files.content.read"
        //    };

        //    var flow = new OidcFlow(type, clientId, clientSecret, scopes, 50001, 50005);
        //    return await flow.AuthorizeOauth2Async("https://www.dropbox.com/oauth2/authorize", "https://api.dropboxapi.com/oauth2/token");

        //}

        public async Task <AccountConfiguration> CreateAccount()
        {
            var f = new OidcWaitForm();

            f.InitEx(_isAccessRestricted ? StorageType.DropboxRestricted : StorageType.Dropbox);
            f.Show();

            var clientId     = _isAccessRestricted ? DropboxHelper.DropboxAppFolderOnlyClientId : DropboxHelper.DropboxFullAccessClientId;
            var clientSecret = _isAccessRestricted ? DropboxHelper.DropboxAppFolderOnlyClientSecret : DropboxHelper.DropboxFullAccessClientSecret;

            string[] scopes =
            {
                "account_info.read",
                "files.metadata.write",
                "files.metadata.read",
                "files.content.write",
                "files.content.read"
            };

            var browser = new OidcSystemBrowser(50001, 50005);

            var redirectUri = browser.RedirectUri;

            var state         = Guid.NewGuid().ToString("N");
            var codeVerifier  = db.DropboxOAuth2Helper.GeneratePKCECodeVerifier();
            var codeChallenge = db.DropboxOAuth2Helper.GeneratePKCECodeChallenge(codeVerifier);
            var uri           = db.DropboxOAuth2Helper.GetAuthorizeUri(db.OAuthResponseType.Code, clientId, redirectUri, state, false, false, null, false, db.TokenAccessType.Offline, scopes, db.IncludeGrantedScopes.None, codeChallenge);

            var query = await browser.GetQueryStringAsync(uri.ToString(), f.CancellationToken);


            var resultState = query["state"];

            if (state != resultState)
            {
                throw new Exception("MiM-Attack?");
            }

            var code = query["code"];

            var response = await db.DropboxOAuth2Helper.ProcessCodeFlowAsync(code, clientId, null, redirectUri, null, codeVerifier);

            var api   = DropboxHelper.GetApi(response.AccessToken);
            var owner = await api.Users.GetCurrentAccountAsync();

            var account = new AccountConfiguration()
            {
                Id     = owner.AccountId,
                Name   = owner.Name.DisplayName,
                Type   = _isAccessRestricted ? StorageType.DropboxRestricted : StorageType.Dropbox,
                Secret = response.RefreshToken,
            };

            f.Close();

            return(account);
        }