Bläddra i källkod

add kakao oauth login(url)

hoparkgo9ma 7 år sedan
förälder
incheckning
1ab79d042f
3 ändrade filer med 52 tillägg och 4 borttagningar
  1. 1 1
      app.py
  2. 5 1
      data/oauthsettings.json
  3. 46 2
      route/login_oauth.py

+ 1 - 1
app.py

@@ -452,7 +452,7 @@ def close_topic_list(name = None, tool = None):
 def login():
     return login_2(conn)
 
-@app.route('/oauth/<regex("discord|naver|facebook"):platform>/<regex("init|callback"):func>', methods=['GET', 'POST'])
+@app.route('/oauth/<regex("discord|naver|facebook|kakao"):platform>/<regex("init|callback"):func>', methods=['GET', 'POST'])
 def login_oauth(platform = None, func = None):
     return login_oauth_2(conn, platform, func)
                 

+ 5 - 1
data/oauthsettings.json

@@ -2,7 +2,7 @@
     "_README" : {
         "en" : "To use the oAuth login feature, you must set the 'publish_url' value to a domain address that includes the HTTPS protocol, and actually support HTTPS connections.",
         "ko" : "oAuth 로그인 기능을 사용하려면 'publish_url' 값을 HTTPS 프로토콜을 포함한 도메인 주소로 설정하고, 실제로 HTTPS 연결을 지원해야 합니다.",
-        "support" : ["discord", "facebook", "naver"]
+        "support" : ["discord", "facebook", "naver", "kakao"]
     },
     "publish_url" : "https://",
     "discord" : {
@@ -16,5 +16,9 @@
     "naver" : {
         "client_id" : "",
         "client_secret" : ""
+    },
+    "kakao" : {
+        "client_id" : "",
+        "client_secret" : ""
     }
 }

+ 46 - 2
route/login_oauth.py

@@ -25,6 +25,10 @@ def login_oauth_2(conn, platform, func):
         api_url['redirect'] = 'https://www.facebook.com/v3.1/dialog/oauth'
         api_url['token'] = 'https://graph.facebook.com/v3.1/oauth/access_token'
         api_url['profile'] = 'https://graph.facebook.com/me'
+    elif platform == 'kakao':
+        api_url['redirect'] = 'https://kauth.kakao.com/oauth/authorize'
+        api_url['token'] = 'https://kauth.kakao.com/oauth/token'
+        api_url['profile'] = 'https://kapi.kakao.com/v1/api/talk/profile'
 
     if func == 'init':
         if oauth_data['client_id'] == '' or oauth_data['client_secret'] == '':
@@ -69,6 +73,11 @@ def login_oauth_2(conn, platform, func):
                 data['redirect_uri'], 
                 data['state']
             ))
+        elif platform == 'kakao':
+            return redirect(api_url['redirect'] + '?client_id={}&redirect_uri={}&response_type=code'.format(
+                data['client_id'], 
+                data['redirect_uri']
+            ))
 
     elif func == 'callback':
         code = flask.request.args.get('code')
@@ -95,7 +104,7 @@ def login_oauth_2(conn, platform, func):
                 'User-Agent': 'Mozilla/5.0'
             }
             token_exchange = urllib.request.Request(
-                'https://discordapp.com/api/oauth2/token',
+                api_url['token'],
                 data = bytes(urllib.parse.urlencode(data).encode()),
                 headers = headers
             )
@@ -107,7 +116,7 @@ def login_oauth_2(conn, platform, func):
                 'Authorization' : 'Bearer ' + token_json['access_token']
             }
             profile_exchange = urllib.request.Request(
-                'https://discordapp.com/api/users/@me',
+                api_url['profile'],
                 headers = headers
             )
             profile_result =  urllib.request.urlopen(profile_exchange).read().decode('utf-8')
@@ -159,6 +168,41 @@ def login_oauth_2(conn, platform, func):
                 'name': profile_result_json['name'], 
                 'picture': profile_result_json['picture']['data']['url']
             }
+        elif platform == 'kakao':
+            data = {
+                'client_id'     : data['client_id'],
+                'client_secret' : data['client_secret'],
+                'grant_type'    : 'authorization_code',
+                'redirect_uri'  : data['redirect_uri'],
+                'code'          : code
+            }
+            headers = {
+                'Content-Type': 'application/x-www-form-urlencoded',
+                'User-Agent': 'Mozilla/5.0'
+            }
+            token_exchange = urllib.request.Request(
+                url['token'],
+                data = bytes(urllib.parse.urlencode(data).encode()),
+                headers = headers
+            )
+            token_result = urllib.request.urlopen(token_exchange).read()
+            token_json = json.loads(token_result)
+
+            headers = {
+                'User-Agent'    : 'Mozilla/5.0',
+                'Authorization' : 'Bearer ' + token_json['access_token']
+            }
+            profile_exchange = urllib.request.Request(
+                url['profile'],
+                headers = headers
+            )
+            profile_result =  urllib.request.urlopen(profile_exchange).read().decode('utf-8')
+            profile_result_json = json.loads(profile_result)
+            stand_json = {
+                'id'        : profile_result_json['account_email'], 
+                'name'      : profile_result_json['nickName'],
+                'picture'   : profile_result_json['profileImageURL']
+            }
         
         if flask.session['referrer'][0:6] == 'change':
             curs.execute('select * from oauth_conn where wiki_id = ? and provider = ?', [flask.session['id'], platform])