title: Extract public key from Google "oauth2/v1/certs" cert in PEM format using Go
body: <p>I fetched the Google cert from: </p>
  
  <blockquote>
    <p><a href="https://www.googleapis.com/oauth2/v1/certs" rel="nofollow">https://www.googleapis.com/oauth2/v1/certs</a></p>
  </blockquote>
  
  <p>but I don't know how to parse the cert in Go and extract the public key and make it aplicable for use in rsa.VerifyPKCS1v15() to verify id token (openID connect) signature. If someone could advise me I would appreciate it. Here is the code what I already have:</p>
  
  <pre><code>res, err := http.Get("https://www.googleapis.com/oauth2/v1/certs")
  if err != nil {
      log.Fatal(err)
      return 
  }
  
  certs, err := ioutil.ReadAll(res.Body)
  res.Body.Close()
  if err != nil {
      log.Fatal(err)
      return 
  }
  //extract kid from token header
  var header interface{}
  log.Printf("Oauth header: %v", headerOauth)
  err = json.Unmarshal([]byte(headerOauth), &amp;header)
  
  token_kid := header.(map[string]interface{})["kid"]
  //get modulus and exponent from the cert
  
  var goCertificate interface{}
  
  err = json.Unmarshal(certs, &amp;goCertificate)    
  
  k := goCertificate.(map[string]interface{})[token_kid.(string)]
  
  google_cert := k.(string)
  block_pub, _ := pem.Decode([]byte(google_cert))
  certInterface, err := x509.ParseCertificates(block_pub.Bytes)
  log.Printf("certInterface: %v", *certInterface.PublicKey)
  //I know the line below is wrong but thats how I usualy parse public keys
  pubkeyInterface, err := x509.ParsePKIXPublicKey(certInterface.Bytes)
  pKey, ok := pubkeyInterface.(*rsa.PublicKey)
  </code></pre>