Click here to Skip to main content
15,887,267 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Edge on 7 Pin
Jeremy Falcon15-Feb-23 5:28
professionalJeremy Falcon15-Feb-23 5:28 
GeneralRe: Edge on 7 Pin
jschell15-Feb-23 6:22
jschell15-Feb-23 6:22 
GeneralRe: Edge on 7 Pin
haughtonomous15-Feb-23 20:08
haughtonomous15-Feb-23 20:08 
GeneralRe: Edge on 7 Pin
dandy7216-Feb-23 4:47
dandy7216-Feb-23 4:47 
GeneralRe: Edge on 7 Pin
jschell16-Feb-23 6:44
jschell16-Feb-23 6:44 
GeneralRe: Edge on 7 Pin
dandy7216-Feb-23 6:59
dandy7216-Feb-23 6:59 
GeneralRe: Edge on 7 Pin
jschell16-Feb-23 7:33
jschell16-Feb-23 7:33 
GeneralMaking simple calls complicated to make them simple again. Pin
Pete O'Hanlon15-Feb-23 1:27
mvePete O'Hanlon15-Feb-23 1:27 
I posted last week[^] about how I was developing a new component which is intended to help you interact with, and administer Keycloak instances. The first operation in Keycloak is the ability to generate an access token for a user; this capability lies at the heart of pretty much every operation. Now, if I were doing this via curl, this would be the command I would issue.
curl \
  -d "client_id=admin-cli" \
  -d "username=admin" \
  -d "password=password" \
  -d "grant_type=password" \
  "http://localhost:8080/realms/master/protocol/openid-connect/token"
All very straightforward, but I want to provide code access to the APIs. Right now, to do the same thing, I have a large number of classes, but the simplicity I was talking about last week allows me to write minimal APIs that look like this.
C#
using Keycloak.Core.Authentication;
using Keycloak.Core.Models;
using Keycloak.Core.Options;
using Microsoft.Extensions.Options;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddHttpClient(); 
builder.Services.AddOptions<KeycloakConnectionOptions>()
    .BindConfiguration("keycloak").
    ValidateDataAnnotations().
    ValidateOnStart();

builder.Services.AddTransient<Authorize>();
builder.Services.AddSingleton(r => r.GetRequiredService<IOptions<KeycloakConnectionOptions>>().Value);
var app = builder.Build();

app.MapGet("/", () => "Hello World");

app.MapGet("/token", async (Authorize authorize) =>
{
    var options = builder.Configuration.GetSection("keycloak").Get<KeycloakConnectionOptions>();
    Token token = await authorize.GetAccessToken(options, "CP", "Master");
    return Results.Ok(token);
});

app.Run();
Behind this, I have a really simple JSON structure:
JSON
"keycloak": {
  "AuthorizationServerUrl": "http://localhost:8080/",
  "Realms": [{
    "Key": "CP",
    "Realm": "CP",
    "SslRequired": "External",
    "Resource": "CP-Test",
    "AuthenticationOptions": [{
      "Key": "Master",
      "AuthenticationType": "Password",
      "Password": {
        "Username": "peter",
        "Password": "peter"
      }
    }]
  }]
}
I love simplicity and I love that fast iterations allow me to turn the code around really quickly, including validating the options to make sure they follow my Keycloak connection rules.

GeneralRe: Making simple calls complicated to make them simple again. Pin
honey the codewitch15-Feb-23 1:51
mvahoney the codewitch15-Feb-23 1:51 
GeneralRe: Making simple calls complicated to make them simple again. Pin
Kenneth Haugland15-Feb-23 3:42
mvaKenneth Haugland15-Feb-23 3:42 
GeneralRe: Making simple calls complicated to make them simple again. Pin
devenv.exe15-Feb-23 2:19
professionaldevenv.exe15-Feb-23 2:19 
GeneralRe: Making simple calls complicated to make them simple again. Pin
honey the codewitch15-Feb-23 4:32
mvahoney the codewitch15-Feb-23 4:32 
GeneralRe: Making simple calls complicated to make them simple again. Pin
Slacker00715-Feb-23 5:09
professionalSlacker00715-Feb-23 5:09 
GeneralRe: Making simple calls complicated to make them simple again. Pin
jschell15-Feb-23 6:25
jschell15-Feb-23 6:25 
GeneralRe: Making simple calls complicated to make them simple again. Pin
Pete O'Hanlon15-Feb-23 7:34
mvePete O'Hanlon15-Feb-23 7:34 
GeneralRe: Making simple calls complicated to make them simple again. Pin
peterkmx15-Feb-23 14:03
professionalpeterkmx15-Feb-23 14:03 
GeneralRe: Making simple calls complicated to make them simple again. Pin
Pete O'Hanlon15-Feb-23 19:45
mvePete O'Hanlon15-Feb-23 19:45 
GeneralRe: Making simple calls complicated to make them simple again. Pin
peterkmx16-Feb-23 2:11
professionalpeterkmx16-Feb-23 2:11 
GeneralRe: Making simple calls complicated to make them simple again. Pin
jmaida15-Feb-23 15:24
jmaida15-Feb-23 15:24 
GeneralRe: Making simple calls complicated to make them simple again. Pin
peterkmx15-Feb-23 16:44
professionalpeterkmx15-Feb-23 16:44 
GeneralRe: Making simple calls complicated to make them simple again. Pin
jmaida15-Feb-23 17:13
jmaida15-Feb-23 17:13 
GeneralRe: Making simple calls complicated to make them simple again. Pin
peterkmx16-Feb-23 1:57
professionalpeterkmx16-Feb-23 1:57 
GeneralRe: Making simple calls complicated to make them simple again. Pin
Pete O'Hanlon15-Feb-23 20:40
mvePete O'Hanlon15-Feb-23 20:40 
GeneralRe: Making simple calls complicated to make them simple again. Pin
Gerry Schmitz15-Feb-23 15:28
mveGerry Schmitz15-Feb-23 15:28 
GeneralCCC 2023-02-15 Pin
Peter_in_278014-Feb-23 21:58
professionalPeter_in_278014-Feb-23 21:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.