{"id":420,"date":"2019-05-14T11:00:32","date_gmt":"2019-05-14T02:00:32","guid":{"rendered":"http:\/\/localhost:8000\/?p=420"},"modified":"2021-01-23T12:31:15","modified_gmt":"2021-01-23T03:31:15","slug":"google-oauth-sample","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2019\/05\/google-oauth-sample.html","title":{"rendered":"Google OAuth \u8a8d\u8a3c\u306e\u5b9f\u88c5\u30b5\u30f3\u30d7\u30eb"},"content":{"rendered":"
Kotlin \/ Ktor\u3067\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u66f8\u3044\u3066\u3044\u3066\u3001Google OAuth\u8a8d\u8a3c\u3092\u5b9f\u88c5\u3057\u305f\u306e\u3060\u304c\u3001\u3060\u3044\u305f\u3044\u5b9f\u88c5\u5b8c\u4e86\u3057\u305f\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u3001 \u516c\u5f0f\u30da\u30fc\u30b8<\/a> \u3092\u767a\u898b\u3057\u3066\u3057\u307e\u3044\u3001\u3053\u306e\u30b3\u30fc\u30c9\u306f\u304a\u8535\u5165\u308a\u306b\u306a\u308b\u3053\u3068\u304c\u6c7a\u5b9a\u3057\u305f\uff08\u6d99\uff09\u3002 <\/p>\n \u4e0a\u8a18\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u56f3\u306e\u901a\u308a\u5b9f\u88c5\u3057\u305f\u304c\u3001\u5168\u90e8\u66f8\u304f\u306e\u3082\u5927\u5909\u306a\u306e\u3067\u3001\u4ee5\u4e0b\u306e\uff12\u70b9\u3060\u3051\u8a18\u8f09\u3057\u3088\u3046\u3068\u601d\u3046\u3002<\/p>\n \u30dd\u30a4\u30f3\u30c8\u306f\u3001\u672a\u8a8d\u8a3c\u3060\u3063\u305f\u3089AuthenticationException\u3092throw\u3057\u3066\u3044\u308b\u3068\u3053\u308d\u3002<\/p>\n Google Sign-In JavaScript client reference<\/a> \u3092\u53c2\u7167\u3057\u306a\u304c\u3089\u5b9f\u88c5\u3057\u305f\u3002<\/p>\n Kotlin \/ Ktor\u3067\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u66f8\u3044\u3066\u3044\u3066\u3001Google OAuth\u8a8d\u8a3c\u3092\u5b9f\u88c5\u3057\u305f\u306e\u3060\u304c\u3001\u3060\u3044\u305f\u3044\u5b9f\u88c5\u5b8c\u4e86\u3057\u305f\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u3001 \u516c\u5f0f\u30da\u30fc\u30b8 \u3092\u767a\u898b\u3057\u3066\u3057\u307e\u3044\u3001\u3053\u306e\u30b3\u30fc\u30c9\u306f\u304a\u8535\u5165\u308a\u306b\u306a\u308b\u3053\u3068\u304c\u6c7a\u5b9a\u3057\u305f\uff08\u6d99\uff09\u3002 \u30b5\u30fc\u30d0\u30b5\u30a4\u30c9\u542b\u3081\u3066\uff11\u65e5\u3050\u3089\u3044\u683c\u95d8\u3057\u3066\u3044\u305f\u306e\u3067\u3001\u305f\u3060\u6368\u3066\u308b\u306e\u3082\u5fcd\u3073\u306a\u304f\u3053\u3053\u306b\u8a18\u8f09\u3057\u3066\u304a\u304f\u3002 \u30b7\u30fc\u30b1\u30f3\u30b9\u56f3 \u4e0a\u8a18\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u56f3\u306e\u901a\u308a\u5b9f\u88c5\u3057\u305f\u304c\u3001\u5168\u90e8\u66f8\u304f\u306e\u3082\u5927\u5909\u306a\u306e\u3067\u3001\u4ee5\u4e0b\u306e\uff12\u70b9\u3060\u3051\u8a18\u8f09\u3057\u3088\u3046\u3068\u601d\u3046\u3002 Application\u304b\u3089Google Servers\u306b\u5bfe\u3057\u3066\u8a8d\u8a3c\u6e08\u307f\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3059\u308b\u51e6\u7406 AuthRedirect\u753b\u9762\u306e\u5b9f\u88c5 Google APIs\u3067OAuth \u8a8d\u8a3c\u7528\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210 \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3059\u308b https:\/\/console.developers.google.com \u306b\u3066\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3002\u4eca\u56de\u306fKotlin Ktor Sample\u3068\u3044\u3046\u540d\u524d\u3067\u4f5c\u6210 <\/span>Continue Reading<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[26,27,28],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/420"}],"collection":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/comments?post=420"}],"version-history":[{"count":1,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/420\/revisions"}],"predecessor-version":[{"id":426,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/420\/revisions\/426"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=420"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\n\u30b5\u30fc\u30d0\u30b5\u30a4\u30c9\u542b\u3081\u3066\uff11\u65e5\u3050\u3089\u3044\u683c\u95d8\u3057\u3066\u3044\u305f\u306e\u3067\u3001\u305f\u3060\u6368\u3066\u308b\u306e\u3082\u5fcd\u3073\u306a\u304f\u3053\u3053\u306b\u8a18\u8f09\u3057\u3066\u304a\u304f\u3002<\/p>\n\u30b7\u30fc\u30b1\u30f3\u30b9\u56f3<\/h2>\n
\n
Google APIs\u3067OAuth \u8a8d\u8a3c\u7528\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210<\/h2>\n
\n
\n
Kotlin Ktor Sample<\/code>\u3068\u3044\u3046\u540d\u524d\u3067\u4f5c\u6210\u3059\u308b\u3002<\/li>\n<\/ul>\n<\/li>\n
\n
\n<\/li>\n<\/ul>\n<\/li>\n\n
\n<\/li>\n<\/ul>\n<\/li>\n\n
\n<\/li>\n
\n<\/li>\n<\/ul>\n<\/li>\nApplication\u304b\u3089Google Servers\u306b\u5bfe\u3057\u3066\u8a8d\u8a3c\u6e08\u307f\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3059\u308b\u51e6\u7406<\/h2>\n
Authentication.kt \u306b\u8a8d\u8a3c\u51e6\u7406\u3092\u5b9f\u88c5\u3059\u308b<\/h3>\n
package sample\n\nfun Application.authenticate(idTokenString: String) {\n val clientId = environment.config.property("app.google.client_id").getString() \/\/ \u3053\u3053\u3067config\u304b\u3089\u5024\u3092\u53d6\u5f97\u3057\u305f\u3044\u306e\u3067\u3001Application\u306b\u95a2\u6570\u3092\u751f\u3084\u3057\u3066\u3044\u308b\n authenticateByGoogle(idTokenString, clientId)\n\n \/\/ \u3053\u3093\u306a\u611f\u3058\u3067\u30a2\u30ab\u30a6\u30f3\u30c8\u306e\u60c5\u5831\u3092\u53d6\u5f97\u3067\u304d\u308b\n \/\/ val idToken: GoogleIdToken = authenticateByGoogle(idTokenString, clientId)\n \/\/ val payload: GoogleIdToken.Payload = idToken.payload\n \/\/ val subject: String = payload.subject\n \/\/ val email: String = payload.email\n}\n\nprivate fun authenticateByGoogle(idTokenString: String, clientId: String): GoogleIdToken {\n val transport = NetHttpTransport()\n val jsonFactory = JacksonFactory.getDefaultInstance()\n val verifier: GoogleIdTokenVerifier = GoogleIdTokenVerifier\n .Builder(transport, jsonFactory)\n .setAudience(Collections.singletonList(clientId))\n .build()\n\n \/\/ \u78ba\u8a8d\u7d50\u679c\u304cnull\u306e\u5834\u5408\u306fAuthenticationException\u3092throw\u3057\u3066\u3044\u308b\n return verifier.verify(idTokenString) ?: throw AuthenticationException()\n}<\/code><\/pre>\n
AuthenticationException \u3092catch\u3057\u305f\u3089AuthRedirect\u753b\u9762\u306bredirect\u3059\u308b\u51e6\u7406\u3092 statusPages \u306b\u8ffd\u52a0\u3059\u308b<\/h3>\n
:\n \/\/ StatusPages\u3092\u8a2d\u5b9a\n install(StatusPages) {\n data class ErrorResponse(val message: String?)\n :\n exception<AuthenticationException> {\n call.respondRedirect("http:\/\/localhost:8080\/static\/authRedirect.html")\n call.respond(HttpStatusCode.Unauthorized)\n }\n exception<Throwable> {\n it.printStackTrace()\n call.respond(HttpStatusCode.InternalServerError, ErrorResponse(it.message))\n }\n }\n :<\/code><\/pre>\n
AuthRedirect\u753b\u9762\uff08authRedirect.html\uff09\u306e\u5b9f\u88c5<\/h2>\n
<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <title>Google OAuth Redirect | Sample<\/title>\n<\/head>\n<body>\n <script src="https:\/\/apis.google.com\/js\/platform.js?onload=init" async defer><\/script>\n <script>\n function init() {\n var redirectUrl = "http:\/\/localhost:8080\/contents\/"; \/\/ \u5b9f\u969b\u306fFreeMarker\u7d4c\u7531\u3067\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u5148\u306eURL\u3092\u53d7\u3051\u53d6\u308a\u8a2d\u5b9a\u3059\u308b\n\n gapi.load('auth2', function() {\n var config = {\n client_id: 'xxxxx.apps.googleusercontent.com', \/\/ \u5b9f\u969b\u306fFreeMarker\u7d4c\u7531\u3067\u74b0\u5883\u5909\u6570\u304b\u3089\u5024\u3092\u53d6\u5f97\u3059\u308b\n scope: 'profile email',\n response_type: 'id_token'\n };\n\n \/\/ OAuth\u540c\u610f\u753b\u9762\u3092\u30dd\u30c3\u30d7\u30a2\u30c3\u30d7\u8868\u793a\u3057\u3066\u8a8d\u8a3c\u3092\u884c\u3046\n gapi.auth2.authorize(config, function(response) {\n if (response.error) {\n alert('Error Occurred!!'); \/\/ \u5b9f\u969b\u306f\u30a8\u30e9\u30fc\u306e\u7a2e\u985e\u306f\u5224\u5225\u3067\u304d\u308b\u306e\u3067\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u5909\u3048\u308b\u65b9\u304c\u3088\u3055\u305d\u3046\n return;\n }\n\n \/\/ idToken\u3092cookie\u306b\u8a2d\u5b9a\u3059\u308b\n var idToken = response.id_token;\n document.cookie = "idToken=" + idToken + "; path=\/; max-age=86400"; \/\/ \u3068\u308a\u3042\u3048\u305a1\u65e5\n\n \/\/ \u5143\u3005\u8868\u793a\u3057\u3088\u3046\u3068\u3057\u3066\u3044\u305f\u753b\u9762\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3059\u308b\n window.location.href = redirectUrl;\n });\n });\n }\n <\/script>\n<\/body>\n<\/html><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"