Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(deps): bump @babel/traverse from 7.22.5 to 7.23.2 in /example/frontend #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 50 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,67 @@

## Usage

在某个神秘的地方注册好 Client,配置 redirect url 并获取 id 和 secret

```go
import(
"github.com/BingyanStudio/oidc-cli/oidc"
)

var oidcConfig = oidc.Config{
ClientID: "xxxx-xx-xxxxxxx-xxxx",
ClientSecret: "xxxxxxxxx",
RedirectURL: "http://example-client.com/callback",
}
var oidcCli = oidc.NewClient(
&oidc.Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "", // Frontend Callback URI
},
)

func OidcCallbackHandler(c echo.Context) error {
code := c.QueryParam("code")
callbackRes, err := oidc.Callback(oidcConfig, code)
func UserTokenHandler(c echo.Context) error {
code := c.QueryParam("code")
tokens, err := oidcCli.RetrieveTokens(code)
if err != nil {
...
}

log.Println(callbackRes.IDToken);
log.Println(callbackRes.Claims.Subject);
log.Println(callbackRes.Claims.Phone);

log.Println(tokens)

... // use the token & claims to further authorization
}

```

具体用法参见 [example](example)

对于获取的返回值`ResponseTokens`:

- IDToken 的 Claims 已解析至 `ResponseTokens.IDTokenClaims`

字段如下

```go
type IDTokenClaims struct {
Issuer string `json:"iss,omitempty"`
IssuedAt int64 `json:"iat,omitempty"` // issue time of token
ExpiresAt int64 `json:"exp,omitempty"` // expire time of token

Audience string `json:"aud,omitempty"` // client id
Nounce string `json:"nounce,omitempty"` // ramdom generated by client
ATHash string `json:"at_hash,omitempty"` // access token hash - for client to validate the access token
Subject string `json:"sub,omitempty"` // user id
Role string `json:"rol,omitempty"` // role of user
SessionId string `json:"sid,omitempty"`

Nickname string `json:"nickname,omitempty"`
Picture string `json:"picture,omitempty"`
Gender string `json:"gender,omitempty"`

Email string `json:"email,omitempty"`
EmailVerified bool `json:"email_verified,omitempty"`
Phone string `json:"phone,omitempty"`
PhoneVerified bool `json:"phone_verified,omitempty"`
Group string `json:"group,omitempty"`
}
```

其中,`sub` 为用户的身份唯一标识符;身份信息的返回取决于 Scope 的设置。

- 库中封装的 Scope 默认为 `[]string{oidc.ScopeOpenID, ScopeProfile, ScopePhone, ScopeEmail}`

- 若要实现用户令牌的刷新,需以适当的方法存储 `ResponseTokens.IDToken` 与 `ResponseTokens.RefreshToken`。当刷新 RP 的 Token 时需确定 IDToken 的有效性。
Loading