public static async Task MainAsync()
        {
            var settings = InitSettings();

            var dcrService    = new DcrService(settings);
            var kjService     = new KjernejournalService(settings);
            var signinService = new SigninService(settings);

            Console.WriteLine("+-----------------------+");
            Console.WriteLine("|      HIDEnabler       |");
            Console.WriteLine("+-----------------------+");
            Console.WriteLine("");

            Console.WriteLine("Signing in with enterprise certificate");
            var accessToken = await signinService.SignIn();

            Console.WriteLine("AccessToken: ");
            Console.WriteLine(accessToken.DecodeToken());

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
            Console.Clear();

            Console.WriteLine("Creating dcr client");

            var client = await dcrService.CreateClient(accessToken, settings.GrantType, settings.RedirectUri, settings.LogoutUri, settings.Scopes.FromSpaceSeparatedToList());

            var clientId = client.ClientId;

            Console.WriteLine("Created client with dcr api:");
            Console.WriteLine("Clientid:     " + client.ClientId);
            Console.WriteLine("GrantType:    " + string.Join(",", client.GrantTypes.ToList()));
            Console.WriteLine("RedirectUris: " + string.Join(",", client.RedirectUris));
            Console.WriteLine("Scopes:       " + string.Join(",", client.AllowedScopes));

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
            Console.Clear();

            var orgNumbers = await kjService.GetOrgNumbers(accessToken);

            // Let user choose orgnumber and return
            var orgNumber = ChooseOrgNumber(orgNumbers);

            await kjService.SetOrgNumber(accessToken, clientId, orgNumber.Nr);

            Console.Clear();
            Console.WriteLine("Signing in with dcr client");
            //Try to log in with the new configuration
            var loginResponse = await signinService.RsaSignInWithAuthCode(clientId, settings.Scopes);

            Console.WriteLine("Signin complete. Recieved access token:");
            Console.WriteLine(loginResponse.AccessToken.DecodeToken());

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
Exemple #2
0
        private void btStoreClient_Click(object sender, EventArgs e)
        {
            btStoreClient.Enabled = false;
            UpdateResult("Performing user authentication...");

            Task.Run(() =>
            {
                try
                {
                    // Fetch a new HelseId Access Token using the authorization code grant type to access secured resources
                    var accessToken = authenticationOperations.IsAuthenticated
                        ? authenticationOperations.AccessToken
                        : authenticationOperations.InitAuthorizationCodeGrant();

                    if (string.IsNullOrWhiteSpace(accessToken))
                    {
                        throw new ArgumentException("No access token.");
                    }

                    UpdateResult("Fetched new access token, calling HelseId Dcr API to register new client...");

                    // Create a new HelseId client configuration
                    var dcrService = new DcrService(new DcrServiceSettings {
                        DcrApi = configurations.HelseIdApiEndpoint
                    });

                    // Register new client
                    var client = dcrService.CreateClient(accessToken, DcrServiceSettings.DefaultGrantTypes, configurations.HelseIdRedirectUrl, null,
                                                         configurations.HelseIdScope.Split(' '), configurations.HelseIdOrganizationId, configurations.HelseIdOrganizationName);

                    var result = client.GetAwaiter().GetResult();

                    if (!string.IsNullOrWhiteSpace(result?.ClientId))
                    {
                        UpdateResult("New client registered, calling HelseId to set Org number...");

                        // Create association between organization and newly created client configuration
                        dcrService.SetOrgNumber(accessToken, result.ClientId, configurations.HelseIdOrganizationId);

                        UpdateResult($"Stored new client with id: {result.ClientId}");
                    }
                    else if (!string.IsNullOrWhiteSpace(result?.Error))
                    {
                        throw new Exception(result.Error);
                    }
                    else
                    {
                        UpdateResult($"Store new client failed");
                    }
                }
                catch (Exception ex)
                {
                    ShowError(ex.ToString());
                }
                finally
                {
                    btStoreClient.Invoke((MethodInvoker) delegate
                    {
                        btStoreClient.Enabled = true;
                    });
                }
            });
        }