Skip to content

update example for get jwt token via sa #275

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

Merged
merged 1 commit into from
Jan 17, 2022
Merged
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
31 changes: 10 additions & 21 deletions ru/iam/operations/iam-token/create-for-sa.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ yc iam create-token

- Go

Пример создания JWT с использованием [jwt-go](https://github.com/dgrijalva/jwt-go):
Пример создания JWT с использованием [golang-jwt](https://github.com/golang-jwt/jwt):

```go
import (
"crypto/rsa"
"io/ioutil"
"time"

"github.com/dgrijalva/jwt-go"
"github.com/golang-jwt/jwt/v4"
)

const (
Expand All @@ -260,13 +260,14 @@ yc iam create-token

// Формирование JWT.
func signedToken() string {
issuedAt := time.Now()
token := jwt.NewWithClaims(ps256WithSaltLengthEqualsHash, jwt.StandardClaims{
Issuer: serviceAccountID,
IssuedAt: issuedAt.Unix(),
ExpiresAt: issuedAt.Add(time.Hour).Unix(),
Audience: "https://iam.api.cloud.yandex.net/iam/v1/tokens",
})
claims := jwt.RegisteredClaims{
Issuer: serviceAccountID,
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
IssuedAt: jwt.NewNumericDate(time.Now()),
NotBefore: jwt.NewNumericDate(time.Now()),
Audience: []string{"https://iam.api.cloud.yandex.net/iam/v1/tokens"},
}
token := jwt.NewWithClaims(jwt.SigningMethodPS256, claims)
token.Header["kid"] = keyID

privateKey := loadPrivateKey()
Expand All @@ -277,18 +278,6 @@ yc iam create-token
return signed
}

// По умолчанию Go RSA PSS использует PSSSaltLengthAuto,
// но на странице https://tools.ietf.org/html/rfc7518#section-3.5 сказано, что
// размер значения соли должен совпадать с размером вывода хеш-функции.
// После исправления https://github.com/dgrijalva/jwt-go/issues/285
// можно будет заменить на jwt.SigningMethodPS256.
var ps256WithSaltLengthEqualsHash = &jwt.SigningMethodRSAPSS{
SigningMethodRSA: jwt.SigningMethodPS256.SigningMethodRSA,
Options: &rsa.PSSOptions{
SaltLength: rsa.PSSSaltLengthEqualsHash,
},
}

func loadPrivateKey() *rsa.PrivateKey {
data, err := ioutil.ReadFile(keyFile)
if err != nil {
Expand Down