diff --git a/404.html b/404.html index e9fbf8c..4c36cb7 100644 --- a/404.html +++ b/404.html @@ -311,7 +311,7 @@
  • - Signing methods + Signing Methods
  • diff --git a/assets/images/social/usage/signing_methods.png b/assets/images/social/usage/signing_methods.png index 3ff2863..8a7fb78 100644 Binary files a/assets/images/social/usage/signing_methods.png and b/assets/images/social/usage/signing_methods.png differ diff --git a/index.html b/index.html index 77587f3..6749ea2 100644 --- a/index.html +++ b/index.html @@ -412,7 +412,7 @@
  • - Signing methods + Signing Methods
  • diff --git a/search/search_index.json b/search/search_index.json index 81bceaf..6920b1c 100644 --- a/search/search_index.json +++ b/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Getting Started","text":"

    Welcome to jwt-go, a go (or 'golang' for search engine friendliness) implementation of JSON Web Tokens.

    "},{"location":"#supported-go-versions","title":"Supported Go versions","text":"

    Our support of Go versions is aligned with Go's version release policy. So we will support a major version of Go until there are two newer major releases. We no longer support building jwt-go with unsupported Go versions, as these contain security vulnerabilities which will not be fixed.

    "},{"location":"#what-the-heck-is-a-jwt","title":"What the heck is a JWT?","text":"

    JWT.io has a great introduction to JSON Web Tokens.

    In short, it's a signed JSON object that does something useful (for example, authentication). It's commonly used for Bearer tokens in OAuth 2.0 A token is made of three parts, separated by .'s. The first two parts are JSON objects, that have been base64url encoded. The last part is the signature, encoded the same way.

    The first part is called the header. It contains the necessary information for verifying the last part, the signature. For example, which encryption method was used for signing and what key was used.

    The part in the middle is the interesting bit. It's called the Claims and contains the actual stuff you care about. Refer to RFC 7519 for information about reserved keys and the proper way to add your own.

    "},{"location":"#whats-in-the-box","title":"What's in the box?","text":"

    This library supports the parsing and verification as well as the generation and signing of JWTs. Current supported signing algorithms are HMAC SHA, RSA, RSA-PSS, ECDSA and EdDSA, though hooks are present for adding your own.

    "},{"location":"#installation-guidelines","title":"Installation Guidelines","text":"

    To install the jwt package, you first need to have Go installed, then you can use the command below to add jwt-go as a dependency in your Go program.

    go get -u github.com/golang-jwt/jwt/v5\n

    Then import it in your code:

    import \"github.com/golang-jwt/jwt/v5\"\n
    "},{"location":"usage/create/","title":"Creating a New JWT","text":"

    One of the primary goals of this library is to create a new JWT (or in short token).

    "},{"location":"usage/create/#with-default-options","title":"With Default Options","text":"

    The easiest way to create a token is to use the jwt.New function. It then needs one of the available signing methods, to finally sign and convert the token into a string format (using the SignedString method). In the first example, we are using a symmetric signing method, i.e., HS256. For a symmetric method, both the signing and the verifying key are the same and thus, both must be equally protected (and should definitely NOT be stored in your code).

    var (\nkey []byte\nt   *jwt.Token\ns   string\n)\nkey = /* Load key from somewhere, for example an environment variable */\nt = jwt.New(jwt.SigningMethodHS256) // (1)!\ns = t.SignedString(key) // (2)!\n
    1. This initializes a new jwt.Token struct based on the supplied signing method. In this case a symmetric method is chosen.
    2. This step computes a cryptographic signature based on the supplied key.

    Signing using an asymmetric signing method (for example ECDSA) works quite similar. For an asymmetric method, the private key (which must be kept secret) is used to sign and the corresponding public key (which can be freely transmitted) is used to verify the token.

    var (\nkey *ecdsa.PrivateKey\nt   *jwt.Token\ns   string\n)\nkey = /* Load key from somewhere, for example a file */\nt = jwt.New(jwt.SigningMethodES256) // (1)!\ns = t.SignedString(key) // (2)!\n
    1. This initializes a new jwt.Token struct based on the supplied signing method. In this case a asymmetric method is chosen.
    2. This step computes a cryptographic signature based on the supplied private key.

    Note, that the chosen signing method and the type of key must match. Please refer to Signing Methods for a complete overview.

    "},{"location":"usage/create/#with-additional-claims","title":"With Additional Claims","text":"

    While the step above using jwt.New creates a valid token, it contains an empty set of claims. Claims are certain pieces of information that are encoded into the token. Since they are encoded and signed by the issuer of the token, one can assume that this information is valid (in the scope of the issuer). Claims can be used to provide the basis for user authentication or authorization, e.g., by encoding a user name or ID or roles into the token. This is also commonly in combination with OAuth 2.0. Furthermore, claims can also contain certain metadata about the token itself, e.g., the time until which the token is regarded as valid and not expired.

    RFC 7519 provides a list of so called registered claim names 1, which each JWT parser needs to understand. Using the jwt.NewWithClaims, a token with different claims can be created.

    var (\nkey *ecdsa.PrivateKey\nt   *jwt.Token\ns   string\n)\nkey = /* Load key from somewhere, for example a file */\nt = jwt.NewWithClaims(jwt.SigningMethodES256, // (1)!\njwt.MapClaims{ // (2)!\n\"iss\": \"my-auth-server\", // (3)!\n\"sub\": \"john\", // (4)!\n\"foo\": 2, // (5)!\n})\ns = t.SignedString(key) // (6)!\n
    1. This initializes a new jwt.Token struct based on the supplied signing method. In this case a asymmetric method is chosen, which is the first parameter.
    2. The second parameter contains the desired claims in form of the jwt.Claims interface. In this case jwt.MapClaims are used, which is a wrapper type around a Go map containing string keys.
    3. The \"sub\"3 claim is a registered claim name that contains the subject this token identifies, e.g. a user name. More technical, this claim identifies the principal that is the subject of the token.
    4. The \"iss\"2 claim is a registered claim name that contains the issuer of the token. More technical, this claim identifies the principal that issued the token.
    5. The \"foo\" claim is a custom claim containing a numeric value. Any string value can be chosen as a claim name, as long as it does not interfere with a registered claim name.
    6. This step computes a cryptographic signature based on the supplied private key.
    "},{"location":"usage/create/#with-options","title":"With Options","text":"

    While we already prepared a jwt.TokenOption type, which can be supplied as a varargs to jwt.New and jwt.NewWithClaims, these are strictly for future compatibility and are currently not used.

    1. Section 4.1 of RFC 7519 \u21a9

    2. Section 4.1.1 of RFC 7519 \u21a9

    3. Section 4.1.2 of RFC 7519 \u21a9

    "},{"location":"usage/parse/","title":"Parsing and Validating a JWT","text":""},{"location":"usage/parse/#keyfunc","title":"Keyfunc","text":""},{"location":"usage/parse/#with-options","title":"With Options","text":"Option Name Arguments Description WithValidMethods methods as []string Supplies a list of signing methods that the parser will check against the algorithm on the token. Only the supplied methods will be considered valid. It is heavily encouraged to use this option in order to prevent \"none\" algorithm attacks.1 WithJSONNumber - Configures the underlying JSON parser to use the UseNumber function, which decodes numeric JSON values into the json.Number type instead of float64. This type can then be used to convert the value into either a floating type or integer type. WithIssuer issuer as string Configures the validator to require the specified issuer in the \"iss\"2 claim. Validation will fail if a different issuer is specified in the token or the \"iss\" claim is missing. WithSubject subject as string Configures the validator to require the specified subject in the \"sub\"3 claim. Validation will fail if a different subject is specified in the token or the \"sub\" claim is missing. WithAudience audience as string Configures the validator to require the specified audience in the \"aud\"4 claim. Validation will fail if the audience is not listed in the token or the \"aud\" claim is missing. The contents of the audience string is application specific, but often contains the URI of the service that consumes the token. WithLeeway leeway as time.Duration According to the RFC, a certain time window (leeway) is allowed when verifying time based claims, such as expiration time. This is due to the fact that a there is not perfect clock synchronization on the a distributed system such as the internet. While we do not enforce any restriction on the amount of leeway, it should generally not exceed more than a few minutes.5 WithIssuedAt - Enables a sanity check of the \"iat\"6 claim. More specifically, when turning this option on, the validator will check if the issued-at time is not in the future. Danger Zone
    1. https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries \u21a9

    2. Section 4.1.1 of RFC 7519 \u21a9

    3. Section 4.1.2 of RFC 7519 \u21a9

    4. Section 4.1.3 of RFC 7519 \u21a9

    5. Section 4.1.4 of RFC 7519 \u21a9

    6. Section 4.1.6 of RFC 7519 \u21a9

    "}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Getting Started","text":"

    Welcome to jwt-go, a go (or 'golang' for search engine friendliness) implementation of JSON Web Tokens.

    "},{"location":"#supported-go-versions","title":"Supported Go versions","text":"

    Our support of Go versions is aligned with Go's version release policy. So we will support a major version of Go until there are two newer major releases. We no longer support building jwt-go with unsupported Go versions, as these contain security vulnerabilities which will not be fixed.

    "},{"location":"#what-the-heck-is-a-jwt","title":"What the heck is a JWT?","text":"

    JWT.io has a great introduction to JSON Web Tokens.

    In short, it's a signed JSON object that does something useful (for example, authentication). It's commonly used for Bearer tokens in OAuth 2.0 A token is made of three parts, separated by .'s. The first two parts are JSON objects, that have been base64url encoded. The last part is the signature, encoded the same way.

    The first part is called the header. It contains the necessary information for verifying the last part, the signature. For example, which encryption method was used for signing and what key was used.

    The part in the middle is the interesting bit. It's called the Claims and contains the actual stuff you care about. Refer to RFC 7519 for information about reserved keys and the proper way to add your own.

    "},{"location":"#whats-in-the-box","title":"What's in the box?","text":"

    This library supports the parsing and verification as well as the generation and signing of JWTs. Current supported signing algorithms are HMAC SHA, RSA, RSA-PSS, ECDSA and EdDSA, though hooks are present for adding your own.

    "},{"location":"#installation-guidelines","title":"Installation Guidelines","text":"

    To install the jwt package, you first need to have Go installed, then you can use the command below to add jwt-go as a dependency in your Go program.

    go get -u github.com/golang-jwt/jwt/v5\n

    Then import it in your code:

    import \"github.com/golang-jwt/jwt/v5\"\n
    "},{"location":"usage/create/","title":"Creating a New JWT","text":"

    One of the primary goals of this library is to create a new JWT (or in short token).

    "},{"location":"usage/create/#with-default-options","title":"With Default Options","text":"

    The easiest way to create a token is to use the jwt.New function. It then needs one of the available signing methods, to finally sign and convert the token into a string format (using the SignedString method). In the first example, we are using a symmetric signing method, i.e., HS256. For a symmetric method, both the signing and the verifying key are the same and thus, both must be equally protected (and should definitely NOT be stored in your code).

    var (\nkey []byte\nt   *jwt.Token\ns   string\n)\nkey = /* Load key from somewhere, for example an environment variable */\nt = jwt.New(jwt.SigningMethodHS256) // (1)!\ns = t.SignedString(key) // (2)!\n
    1. This initializes a new jwt.Token struct based on the supplied signing method. In this case a symmetric method is chosen.
    2. This step computes a cryptographic signature based on the supplied key.

    Signing using an asymmetric signing method (for example ECDSA) works quite similar. For an asymmetric method, the private key (which must be kept secret) is used to sign and the corresponding public key (which can be freely transmitted) is used to verify the token.

    var (\nkey *ecdsa.PrivateKey\nt   *jwt.Token\ns   string\n)\nkey = /* Load key from somewhere, for example a file */\nt = jwt.New(jwt.SigningMethodES256) // (1)!\ns = t.SignedString(key) // (2)!\n
    1. This initializes a new jwt.Token struct based on the supplied signing method. In this case a asymmetric method is chosen.
    2. This step computes a cryptographic signature based on the supplied private key.

    Note, that the chosen signing method and the type of key must match. Please refer to Signing Methods for a complete overview.

    "},{"location":"usage/create/#with-additional-claims","title":"With Additional Claims","text":"

    While the step above using jwt.New creates a valid token, it contains an empty set of claims. Claims are certain pieces of information that are encoded into the token. Since they are encoded and signed by the issuer of the token, one can assume that this information is valid (in the scope of the issuer). Claims can be used to provide the basis for user authentication or authorization, e.g., by encoding a user name or ID or roles into the token. This is also commonly in combination with OAuth 2.0. Furthermore, claims can also contain certain metadata about the token itself, e.g., the time until which the token is regarded as valid and not expired.

    RFC 7519 provides a list of so called registered claim names 1, which each JWT parser needs to understand. Using the jwt.NewWithClaims, a token with different claims can be created.

    var (\nkey *ecdsa.PrivateKey\nt   *jwt.Token\ns   string\n)\nkey = /* Load key from somewhere, for example a file */\nt = jwt.NewWithClaims(jwt.SigningMethodES256, // (1)!\njwt.MapClaims{ // (2)!\n\"iss\": \"my-auth-server\", // (3)!\n\"sub\": \"john\", // (4)!\n\"foo\": 2, // (5)!\n})\ns = t.SignedString(key) // (6)!\n
    1. This initializes a new jwt.Token struct based on the supplied signing method. In this case a asymmetric method is chosen, which is the first parameter.
    2. The second parameter contains the desired claims in form of the jwt.Claims interface. In this case jwt.MapClaims are used, which is a wrapper type around a Go map containing string keys.
    3. The \"sub\"3 claim is a registered claim name that contains the subject this token identifies, e.g. a user name. More technical, this claim identifies the principal that is the subject of the token.
    4. The \"iss\"2 claim is a registered claim name that contains the issuer of the token. More technical, this claim identifies the principal that issued the token.
    5. The \"foo\" claim is a custom claim containing a numeric value. Any string value can be chosen as a claim name, as long as it does not interfere with a registered claim name.
    6. This step computes a cryptographic signature based on the supplied private key.
    "},{"location":"usage/create/#with-options","title":"With Options","text":"

    While we already prepared a jwt.TokenOption type, which can be supplied as a varargs to jwt.New and jwt.NewWithClaims, these are strictly for future compatibility and are currently not used.

    1. Section 4.1 of RFC 7519 \u21a9

    2. Section 4.1.1 of RFC 7519 \u21a9

    3. Section 4.1.2 of RFC 7519 \u21a9

    "},{"location":"usage/parse/","title":"Parsing and Validating a JWT","text":""},{"location":"usage/parse/#keyfunc","title":"Keyfunc","text":""},{"location":"usage/parse/#with-options","title":"With Options","text":"Option Name Arguments Description WithValidMethods methods as []string Supplies a list of signing methods that the parser will check against the algorithm on the token. Only the supplied methods will be considered valid. It is heavily encouraged to use this option in order to prevent \"none\" algorithm attacks.1 WithJSONNumber - Configures the underlying JSON parser to use the UseNumber function, which decodes numeric JSON values into the json.Number type instead of float64. This type can then be used to convert the value into either a floating type or integer type. WithIssuer issuer as string Configures the validator to require the specified issuer in the \"iss\"2 claim. Validation will fail if a different issuer is specified in the token or the \"iss\" claim is missing. WithSubject subject as string Configures the validator to require the specified subject in the \"sub\"3 claim. Validation will fail if a different subject is specified in the token or the \"sub\" claim is missing. WithAudience audience as string Configures the validator to require the specified audience in the \"aud\"4 claim. Validation will fail if the audience is not listed in the token or the \"aud\" claim is missing. The contents of the audience string is application specific, but often contains the URI of the service that consumes the token. WithLeeway leeway as time.Duration According to the RFC, a certain time window (leeway) is allowed when verifying time based claims, such as expiration time. This is due to the fact that a there is not perfect clock synchronization on the a distributed system such as the internet. While we do not enforce any restriction on the amount of leeway, it should generally not exceed more than a few minutes.5 WithIssuedAt - Enables a sanity check of the \"iat\"6 claim. More specifically, when turning this option on, the validator will check if the issued-at time is not in the future. Danger Zone
    1. https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries \u21a9

    2. Section 4.1.1 of RFC 7519 \u21a9

    3. Section 4.1.2 of RFC 7519 \u21a9

    4. Section 4.1.3 of RFC 7519 \u21a9

    5. Section 4.1.4 of RFC 7519 \u21a9

    6. Section 4.1.6 of RFC 7519 \u21a9

    "},{"location":"usage/signing_methods/","title":"Signing Methods","text":"

    There are several signing methods available, and you should probably take the time to learn about the various options before choosing one. The principal design decision is most likely going to be symmetric vs asymmetric.

    Symmetric signing methods, such as HMAC, use only a single secret. This is probably the simplest signing method to use since any []byte can be used as a valid secret. They are also slightly computationally faster to use, though this rarely is enough to matter. Symmetric signing methods work the best when both producers and consumers of tokens are trusted, or even the same system. Since the same secret is used to both sign and validate tokens, you can't easily distribute the key for validation.

    Asymmetric signing methods, such as RSA, use different keys for signing and verifying tokens. This makes it possible to produce tokens with a private key, and allow any consumer to access the public key for verification.

    "},{"location":"usage/signing_methods/#signing-methods-and-key-types","title":"Signing Methods and Key Types","text":"

    Each signing method expects a different object type for its signing keys. The following table lists all supported signing methods in the core library.

    Name \"alg\" Parameter Values Signing Key Type Verification Key Type HMAC signing method1 HS256,HS384,HS512 []byte []byte RSA signing method2 RS256,RS384,RS512 *rsa.PrivateKey *rsa.PublicKey ECDSA signing method3 ES256,ES384,ES512 *ecdsa.PrivateKey *ecdsa.PublicKey RSA-PSS signing method4 PS256,PS384,PS512 *rsa.PrivateKey *rsa.PublicKey EdDSA signing method5 Ed25519 ed25519.PrivateKey ed25519.PublicKey
    1. Section 3.2 of RFC 7518 \u21a9

    2. Section 3.2 of RFC 7518 \u21a9

    3. Section 3.2 of RFC 7518 \u21a9

    4. Section 3.2 of RFC 7518 \u21a9

    5. Section 3.1 of RFC 8037 \u21a9

    "}]} \ No newline at end of file diff --git a/sitemap.xml b/sitemap.xml index 8ecb3e6..6370336 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -2,22 +2,22 @@ https://golang-jwt.github.io/jwt/ - 2023-03-26 + 2023-03-30 daily https://golang-jwt.github.io/jwt/usage/create/ - 2023-03-26 + 2023-03-30 daily https://golang-jwt.github.io/jwt/usage/parse/ - 2023-03-26 + 2023-03-30 daily https://golang-jwt.github.io/jwt/usage/signing_methods/ - 2023-03-26 + 2023-03-30 daily \ No newline at end of file diff --git a/sitemap.xml.gz b/sitemap.xml.gz index 964fcb1..e1f9af3 100644 Binary files a/sitemap.xml.gz and b/sitemap.xml.gz differ diff --git a/usage/create/index.html b/usage/create/index.html index 152333a..fe47600 100644 --- a/usage/create/index.html +++ b/usage/create/index.html @@ -407,7 +407,7 @@
  • - Signing methods + Signing Methods
  • diff --git a/usage/parse/index.html b/usage/parse/index.html index c82e043..31a9e38 100644 --- a/usage/parse/index.html +++ b/usage/parse/index.html @@ -400,7 +400,7 @@
  • - Signing methods + Signing Methods
  • diff --git a/usage/signing_methods/index.html b/usage/signing_methods/index.html index 6cdc062..2456df8 100644 --- a/usage/signing_methods/index.html +++ b/usage/signing_methods/index.html @@ -19,7 +19,7 @@ - Signing methods - golang-jwt docs + Signing Methods - golang-jwt docs @@ -51,7 +51,7 @@ - + @@ -67,7 +67,7 @@ - + @@ -92,6 +92,11 @@
    + + + Skip to content + +
    @@ -120,7 +125,7 @@
    - Signing methods + Signing Methods
    @@ -350,11 +355,44 @@ + + + + - Signing methods + Signing Methods + + + + @@ -381,9 +419,74 @@ -

    Signing methods

    - - +

    Signing Methods

    +

    There are several signing methods available, and you should probably take the time to learn about the various options before choosing one. The principal design decision is most likely going to be symmetric vs asymmetric.

    +

    Symmetric signing methods, such as HMAC, use only a single secret. This is probably the simplest signing method to use since any []byte can be used as a valid secret. They are also slightly computationally faster to use, though this rarely is enough to matter. Symmetric signing methods work the best when both producers and consumers of tokens are trusted, or even the same system. Since the same secret is used to both sign and validate tokens, you can't easily distribute the key for validation.

    +

    Asymmetric signing methods, such as RSA, use different keys for signing and verifying tokens. This makes it possible to produce tokens with a private key, and allow any consumer to access the public key for verification.

    +

    Signing Methods and Key Types

    +

    Each signing method expects a different object type for its signing keys. The following table lists all supported signing methods in the core library.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Name"alg" Parameter ValuesSigning Key TypeVerification Key Type
    HMAC signing method1HS256,HS384,HS512[]byte[]byte
    RSA signing method2RS256,RS384,RS512*rsa.PrivateKey*rsa.PublicKey
    ECDSA signing method3ES256,ES384,ES512*ecdsa.PrivateKey*ecdsa.PublicKey
    RSA-PSS signing method4PS256,PS384,PS512*rsa.PrivateKey*rsa.PublicKey
    EdDSA signing method5Ed25519ed25519.PrivateKeyed25519.PublicKey
    +
    +
    +
      +
    1. +

      Section 3.2 of RFC 7518 

      +
    2. +
    3. +

      Section 3.2 of RFC 7518 

      +
    4. +
    5. +

      Section 3.2 of RFC 7518 

      +
    6. +
    7. +

      Section 3.2 of RFC 7518 

      +
    8. +
    9. +

      Section 3.1 of RFC 8037 

      +
    10. +
    +