Skip to content

Commit 0594522

Browse files
kakao-joo-yoonjoostory
authored andcommitted
ExternalOAuth1 추가
1 parent 8404985 commit 0594522

File tree

7 files changed

+119
-43
lines changed

7 files changed

+119
-43
lines changed

src/main/apis/tistory-api.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
const uuid = require('uuid').v4
12
const fs = require('fs')
23
const fetch = require('isomorphic-fetch')
34
const querystring = require('querystring')
45
const FormData = require('form-data')
56
const {session} = require('electron')
67
const stream = require('stream')
7-
const Oauth2infoReader = require('../oauth/OauthInfoReader')
8+
const OauthInfoReader = require('../oauth/OauthInfoReader')
89
const appInfo = require('../appInfo')
910
const ExternalOAuth2 = require('../oauth/ExternalOAuth2');
11+
const OAuthRequestManager = require('../oauth/OAuthRequestManager');
1012

1113
const errorHandler = (res) => {
1214
if (!res.ok) {
@@ -19,18 +21,30 @@ const errorHandler = (res) => {
1921
}
2022

2123
const BASE_URL = 'https://www.tistory.com/apis'
24+
const PROVIDER_ID = 'tistory'
2225

23-
const requestAuth = () => {
24-
const oauth2infoReader = new Oauth2infoReader()
25-
const tistoryOAuth = new ExternalOAuth2(oauth2infoReader.getTistory())
26-
tistoryOAuth.requestAuth({})
27-
return tistoryOAuth.getState()
28-
}
26+
const requestAuth = (successHandler, failureHandler) => {
27+
const oauthInfoReader = new OauthInfoReader()
28+
const oauth2 = new ExternalOAuth2(oauthInfoReader.getTistory())
29+
OAuthRequestManager.saveRequestInfo("oauth", (searchParams) => {
30+
const code = searchParams.get("code")
31+
oauth2.requestToken(code, 'GET')
32+
.then(data => {
33+
if (data.error) {
34+
throw new Error(`${data.error}: ${data.error_description}`)
35+
}
36+
37+
return {
38+
uuid: uuid(),
39+
provider: PROVIDER_ID,
40+
authInfo: data
41+
}
42+
})
43+
.then(successHandler)
44+
.catch(failureHandler)
45+
})
2946

30-
const requestToken = (code) => {
31-
const oauth2infoReader = new Oauth2infoReader()
32-
const tistoryOAuth = new ExternalOAuth2(oauth2infoReader.getTistory())
33-
return tistoryOAuth.requestToken(code, 'GET')
47+
oauth2.requestAuth({})
3448
}
3549

3650
const fetchBlogInfo = (auth) => {
@@ -252,7 +266,6 @@ const fetchAccount = async (auth) => {
252266

253267
module.exports = {
254268
requestAuth: requestAuth,
255-
requestToken: requestToken,
256269
fetchBlogInfo: fetchBlogInfo,
257270
fetchUser: fetchUser,
258271
fetchPosts: fetchPosts,

src/main/apis/tumblr-api.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1+
const uuid = require('uuid').v4
12
const OauthInfoReader = require('../oauth/OauthInfoReader')
23
const oauth = require("../oauth/ElectronOauth1")
34
const {session, BrowserWindow} = require('electron')
45
const tumblr = require("tumblr.js")
6+
const ExternalOAuth1 = require('../oauth/ExternalOAuth1');
7+
const OAuthRequestManager = require('../oauth/OAuthRequestManager');
8+
9+
const PROVIDER_ID = 'tumblr'
10+
11+
const requestAuth = (successHandler, failureHandler) => {
12+
const oauthInfoReader = new OauthInfoReader()
13+
const oauth1 = new ExternalOAuth1(oauthInfoReader.getTumblr())
14+
oauth1.requestAuth((requestTokens) => {
15+
OAuthRequestManager.saveRequestInfo('oauth', (searchParams) => {
16+
const verifier = searchParams.get("oauth_verifier")
17+
oauth1.requestToken(verifier, requestTokens, (error, token, tokenSecret) => {
18+
if (error) {
19+
failureHandler()
20+
return
21+
}
22+
23+
successHandler({
24+
uuid: uuid(),
25+
provider: PROVIDER_ID,
26+
authInfo: {
27+
token, tokenSecret
28+
}
29+
})
30+
})
31+
})
32+
})
33+
34+
}
535

636
const getAccessToken = () => {
737
const window = new BrowserWindow({
@@ -158,6 +188,7 @@ const fetchAccount = async (auth) => {
158188

159189

160190
module.exports = {
191+
requestAuth: requestAuth,
161192
getAccessToken: getAccessToken,
162193
fetchUser: fetchUser,
163194
fetchPosts: fetchPosts,

src/main/events/auth.js

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,15 @@ module.exports = () => {
3737
ipcMain.on("request-auth", (evt, provider) => {
3838
console.log('Main.receive: request-auth', provider)
3939
let providerApi = ProviderApiManager.getApi(provider)
40-
let state = providerApi.requestAuth()
41-
OAuthRequestManager.saveRequestInfo(state, (code) => {
42-
const providerApi = ProviderApiManager.getApi(provider)
43-
providerApi.requestToken(code)
44-
.then(data => {
45-
if (data.error) {
46-
throw new Error(`${data.error}: ${data.error_description}`)
47-
}
48-
49-
return {
50-
uuid: uuid(),
51-
provider: provider,
52-
authInfo: data
53-
}
54-
})
55-
.then(saveAuth)
56-
.then(auth => {
57-
providerApi.fetchAccount(auth)
58-
.then(account => {
59-
evt.sender.send('receive-account', account)
60-
})
61-
})
62-
.catch(e => {
63-
console.error(e)
64-
evt.sender.send('receive-message', `오류가 발생했습니다. (${e.message})`)
40+
providerApi.requestAuth((auth) => {
41+
saveAuth(auth)
42+
providerApi.fetchAccount(auth)
43+
.then(account => {
44+
evt.sender.send('receive-account', account)
6545
})
46+
}, e => {
47+
console.error(e)
48+
evt.sender.send('receive-message', `오류가 발생했습니다. (${e.message})`)
6649
})
6750
})
6851

src/main/lib/AuthenticationManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let initialized = false
44
let authList = []
55

66
function load() {
7-
authList = settings.getSync('authList', [])
7+
authList = settings.getSync('authList', []).filter(auth => auth != null)
88
console.log("load authList", authList)
99
}
1010

src/main/main.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ app.on('window-all-closed', () => {
2727

2828
app.on("open-url", (e, urlString) => {
2929
const url = new URL(urlString)
30-
console.log("OPEN_URL", urlString, url, url.pathname, url.searchParams.get('code'), url.searchParams.get('state'))
31-
const requestHandler = OAuthRequestManager.loadRequestInfo(url.searchParams.get('state'))
30+
let requestHandler = OAuthRequestManager.loadRequestInfo("oauth")
3231
if (requestHandler) {
33-
requestHandler(url.searchParams.get('code'))
32+
requestHandler(url.searchParams)
3433
}
3534
})
3635

src/main/oauth/ExternalOAuth1.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const electron = require('electron');
2+
const queryString = require('querystring');
3+
const OAuth = require('oauth').OAuth
4+
5+
class ExternalOAuth2 {
6+
#oauthInfo
7+
#oauth
8+
#requestTokens
9+
10+
constructor(oauthInfo) {
11+
this.#oauthInfo = oauthInfo
12+
this.#oauth = new OAuth(
13+
oauthInfo.requestTokenUrl,
14+
oauthInfo.tokenUrl,
15+
oauthInfo.clientKey,
16+
oauthInfo.clientSecret,
17+
'1.0a',
18+
oauthInfo.redirectUri,
19+
'HMAC-SHA1'
20+
)
21+
}
22+
23+
requestAuth(successHandler) {
24+
this.#oauth.getOAuthRequestToken((error, token, tokenSecret) => {
25+
if (error) {
26+
throw new Error(error)
27+
}
28+
29+
this.#requestTokens = {
30+
token, tokenSecret
31+
}
32+
33+
electron.shell.openExternal(this.#oauthInfo.authorizeUrl + '?' + queryString.stringify({
34+
oauth_token: token
35+
}))
36+
37+
successHandler(this.#requestTokens)
38+
})
39+
}
40+
41+
requestToken(verifier, requestTokens, tokenHandler) {
42+
this.#oauth.getOAuthAccessToken(
43+
requestTokens.token,
44+
requestTokens.tokenSecret,
45+
verifier,
46+
tokenHandler
47+
)
48+
}
49+
50+
}
51+
52+
module.exports = ExternalOAuth2

src/main/oauth/ExternalOAuth2.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ class ExternalOAuth2 {
5959
fetchOptions['body'] = queryString.stringify(tokenRequestData);
6060
}
6161

62-
console.log("requestToken", tokenMethod, url, fetchOptions)
63-
6462
return fetch(url, fetchOptions)
6563
.then(res => res.json())
6664
}

0 commit comments

Comments
 (0)