generateCodeVerifier creates a cryptographically secure random string which is between 43 and 128 characters long using the specified charset.
()
| 135 | // generateCodeVerifier creates a cryptographically secure random string |
| 136 | // which is between 43 and 128 characters long using the specified charset. |
| 137 | func generateCodeVerifier() (string, error) { |
| 138 | // Generate a random number between 0 and 85 to extend the length of the code verifier |
| 139 | r, err := rand.Int(rand.Reader, big.NewInt(86)) |
| 140 | if err != nil { |
| 141 | return "", err |
| 142 | } |
| 143 | // Define the length of the code verifier |
| 144 | // Here, we randomly choose a length between 43 and 128 characters |
| 145 | n := 43 + int(r.Int64()) |
| 146 | |
| 147 | // Create a byte slice of length n to store the characters of our code verifier |
| 148 | b := make([]byte, n) |
| 149 | // Temp slice to read random numbers into |
| 150 | temp := make([]byte, n) |
| 151 | if _, err := rand.Read(temp); err != nil { |
| 152 | return "", err |
| 153 | } |
| 154 | |
| 155 | // Assign a valid character from charset for each byte in b |
| 156 | for i := 0; i < n; i++ { |
| 157 | b[i] = charset[temp[i]%byte(len(charset))] |
| 158 | } |
| 159 | |
| 160 | return string(b), nil |
| 161 | } |
| 162 | |
| 163 | // createCodeChallenge takes a codeVerifier and returns its SHA256 hash |
| 164 | // encoded in Base64 URL encoding without padding, which is the code challenge. |
no outgoing calls