Keycloak adaptor for golang application

前端 未结 2 1064
眼角桃花
眼角桃花 2021-01-31 00:07

I am going to secure my golang application using keycloak, but keycloak itself does not support go language.

There are some go adaptor as an open project in github that

2条回答
  •  面向向阳花
    2021-01-31 00:37

    There is also the gocloak library which provides lot's of functionality. The lib is in active development and allready in use in real world projects. So possible bugs & feature requests are being handled.

    It provides administration features like "CreateUser","CreateGroup" etc. and also provides functions for Login, Token validation, etc.

    For example creating a user is as easy as:

    client := gocloak.NewClient("https://mycool.keycloak.instance")
    token, err := client.LoginAdmin("user", "password", "realmName")
    if err != nil {
        panic("Something wrong with the credentials or url")
    }
    user := gocloak.User{
        FirstName: "Bob",
        LastName:  "Uncle",
        EMail:     "something@really.wrong",
        Enabled:   true,
        Username:  "CoolGuy",
    }
    client.CreateUser(token.AccessToken, "realm", user)
    if err != nil {
        panic("Oh no!, failed to create user :(")
    }
    

    It does also supports Introspecting a Requesting Party Token

    client := gocloak.NewClient(hostname)
    token, err := client.LoginClient(clientid, clientSecret, realm)
    if err != nil {
        panic("Login failed:"+ err.Error())
    }
    
    rptResult, err := client.RetrospectToken(token.AccessToken, clientid, clientSecret, realm)
    if err != nil {
        panic("Inspection failed:"+ err.Error())
    }
    
    if !rptResult.Active {
        panic("Token is not active")
    }
    
    permissions := rptResult.Permissions
    //Do something with the permissions ;) 
    

    Also to handle easy authentication & token refresh when using echo there is another lib based on gocloak called gocloak-echo. This lib provides handler & middleware to help out, but is still in a more WIP state.

    The library also provides decoding of accessTokens into custom claims

    Disclosure: I am the (main) author of gocloak, so it's also a little advertising, but in general it answers the question. I had the same problem as the author and i decided to create my own lib (based on the lib of someone else, as stated in the readme on github).

提交回复
热议问题