File: api-go.md

package info (click to toggle)
oidc-agent 4.2.6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,560 kB
  • sloc: ansic: 22,657; makefile: 711; sh: 494
file content (199 lines) | stat: -rw-r--r-- 5,306 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# liboidcagent-go

A `go` library for `oidc-agent` is available at
https://github.com/indigo-dc/liboidcagent-go.

To use it in your `go` application include:

```go
import "github.com/indigo-dc/liboidcagent-go"
```

## Requesting an Access Token

The following functions can be used to obtain an access token for a specific account configuration or for a given OpenID
provider from `oidc-agent`.

### Token Request

All functions take a `TokenRequest` struct. This struct describes the request:

```go
// 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)
	ShortName string
	// IssuerURL for which an access token should be obtained (Can be omitted
	// if ShortName is specified)
	IssuerURL string
	// MinValidPeriod specifies how long the access token should be valid at
	// least. The time is given in seconds. Default is 0.
	MinValidPeriod uint64
	// The scopes for the requested access token
	Scopes []string
	// The audiences for the requested access token
	Audiences []string
	// A string describing the requesting application (i.e. its name). It might
	// be displayed to the user, if the request must be confirmed or an account
	// configuration loaded.
	ApplicationHint string
}
```

### GetAccessToken

```go
func GetAccessToken(req TokenRequest) (token string, err error)
```

This function requests an access token from oidc-agent according to the passed
[`TokenRequest`](#token-request).

#### Return Value

The function returns only the access token as a `string` and an error. To additionally obtain other information
use [`GetTokenResponse`](#gettokenresponse). On failure an error is returned.

#### Examples

##### Account Configuration Example

A complete example can look the following:

```go
token, err := liboidcagent.GetAccessToken(liboidcagent.TokenRequest{
  ShortName: accountName,
  MinValidPeriod: 60,
  Scopes: []string{"openid", "profile"},
  ApplicationHint: "Example-App",
})
if err != nil {
    var agentError *OIDCAgentError
    if errors.As(err, &agentError) {
        fmt.Printf("%s\n", agentError.ErrorWithHelp())
    }
    // Additional error handling
} else {
    fmt.Printf("Access token is: %s\n", token)
}
```

##### Issuer Example

A complete example can look the following:

```go
token, err := liboidcagent.GetAccessToken(liboidcagent.TokenRequest{
  IssuerURL: "https://openid.example.com",
  MinValidPeriod: 60,
  Scopes: []string{"openid", "profile"},
  ApplicationHint: "Example-App",
})
if err != nil {
    var agentError *OIDCAgentError
    if errors.As(err, &agentError) {
        fmt.Printf("%s\n", agentError.ErrorWithHelp())
    }
    // Additional error handling
} else {
    fmt.Printf("Access token is: %s\n", token)
}
```

### GetTokenResponse

```go
func GetTokenResponse(req TokenRequest) (resp TokenResponse, err error)
```

This function requests an access token from oidc-agent according to the passed
[`TokenRequest`](#token-request).

#### Return Value

The function returns an `TokenResponse struct` that contains the requested access token, the url of the issuer that
issued the token and the time when the token expires.

The values can be accessed the following way:

```go
response, err := liboidcagent.GetTokenResponse(...)
response.Token      // access token
response.Issuer     // issuer url
response.ExpiresAt  // expiration time
```

#### Examples

##### Account Configuration Example

A complete example can look the following:

```go
resp, err := liboidcagent.GetTokenResponse(liboidcagent.TokenRequest{
  ShortName: accountName,
  MinValidPeriod: 60,
  Audiences: []string{"https://storage.example.org"},
  ApplicationHint: "Example-App",
})
if err != nil {
  var agentError *OIDCAgentError
  if errors.As(err, &agentError) {
    fmt.Printf("%s\n", agentError.ErrorWithHelp())
  }
  // Additional error handling
} else {
  fmt.Printf("Access token is: %s\n", resp.Token)
  fmt.Printf("Issuer url is: %s\n", resp.Issuer)
  fmt.Printf("The token expires at: %s\n", resp.ExpiresAt)
}
```

##### Issuer Example

A complete example can look the following:

```go
resp, err := liboidcagent.GetTokenResponse(liboidcagent.TokenRequest{
  Issuer: "https://openid.example.com",
  MinValidPeriod: 60,
  Audiences: []string{"https://storage.example.org"},
  ApplicationHint: "Example-App",
})
if err != nil {
  var agentError *OIDCAgentError
  if errors.As(err, &agentError) {
    fmt.Printf("%s\n", agentError.ErrorWithHelp())
  }
  // Additional error handling
} else {
  fmt.Printf("Access token is: %s\n", resp.Token)
  fmt.Printf("Issuer url is: %s\n", resp.Issuer)
  fmt.Printf("The token expires at: %s\n", resp.ExpiresAt)
}
```

## Getting Loaded Accounts

```go
func GetLoadedAccounts() (accountNames []string, err error) {
```

This function requests the list of currently loaded accounts from oidc-agent.

#### Return Value

The function returns a list of the currently loaded accounts as a `[]string` on success and an `OIDCAgentError` on
failure.

## Getting Configured Accounts

```go
func GetConfiguredAccounts() (accounts []string) {
```

This function checks the oidc-agent directory for the configured accounts.

#### Return Value

The function returns a list of the configured accounts as a `[]string`.