Skip to content

Commit

Permalink
add getAccountInfos and change GetConfiguredAccounts
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmann committed Jun 13, 2023
1 parent 56922d2 commit 6bb14c3
Showing 1 changed file with 63 additions and 10 deletions.
73 changes: 63 additions & 10 deletions liboidcagent.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package liboidcagent

import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"time"

"github.com/adrg/xdg"
mytoken "github.com/oidc-mytoken/api/v0"
)

Expand All @@ -30,6 +29,17 @@ type MytokenResponse struct {
ExpiresAt time.Time
}

// AccountInfoResponse holds information about the available accounts and issuers
type AccountInfoResponse map[string]IssuerInfo

// IssuerInfo is a type for holding information about a supported issuer
type IssuerInfo struct {
// Indicates whether a public client for this issuer is available or not
HasPubClient bool `json:"pubclient"`
// Maps account short names to a bool indicating if this account is currently loaded or not
Accounts map[string]bool `json:"accounts"`
}

// TokenRequest is used to request an access token from the agent
type TokenRequest struct {
// ShortName that should be used (Can be omitted if IssuerURL is specified)
Expand Down Expand Up @@ -76,6 +86,13 @@ type tokenResponse struct {
Help string `json:"info,omitempty"`
}

type infoResponse struct {
Info json.RawMessage `json:"info"`

Status string `json:"status,omitempty"`
Error string `json:"error,omitempty"`
}

type tokenRequest struct {
Request string `json:"request"`
AccountName string `json:"account,omitempty"`
Expand Down Expand Up @@ -244,16 +261,52 @@ func GetLoadedAccounts() (accountNames []string, err error) {
}

// GetConfiguredAccounts returns a list of all accounts which are configured for oidc-agent
func GetConfiguredAccounts() (accounts []string) {
accounts = []string{}
infos, err := ioutil.ReadDir(xdg.ConfigHome + "/oidc-agent")
func GetConfiguredAccounts() (accounts []string, err error) {
accountInfo, err := GetAccountInfos()
if err != nil {
return
return nil, err
}
for _, info := range infos {
if info.Name() != "issuer.config" && !info.IsDir() {
accounts = append(accounts, info.Name())
for _, i := range accountInfo {
for a := range i.Accounts {
accounts = append(accounts, a)
}
}
return accounts
return
}

func getAccountInfos() (accountInfos AccountInfoResponse, err error) {
conn, err := newEncryptedConn()
if err != nil {
return
}
defer conn.close()

req := map[string]string{"request": "account_info"}
var resp infoResponse

err = conn.sendJSONRequest(req, &resp)
if err != nil {
return
}

if resp.Status != "success" {
err = fmt.Errorf("error on account info request (status: %s): %s", resp.Status, resp.Error)
return
}
err = json.Unmarshal(resp.Info, &accountInfos)
if err != nil {
err = fmt.Errorf("error on account info request: account info malformed: %s", err.Error())
return
}
return
}

// GetAccountInfos returns information about all issuers and their available account names and if those are loaded or
// not
func GetAccountInfos() (info AccountInfoResponse, err error) {
info, err = getAccountInfos()
if err != nil {
err = oidcAgentErrorWrap(err)
}
return
}

0 comments on commit 6bb14c3

Please sign in to comment.