|
| 1 | +<!---# Authentication--> |
| 2 | +# 인증 |
| 3 | + |
| 4 | +<!--Users can be authenticated in two ways: over the websocket or over HTTP.--> |
| 5 | +인증은 웹소켓이나 HTTP으로 할수 있습니다. |
| 6 | + |
| 7 | +<!--The first option is useful if you're authenticating against a backend database or other resource you control, the second if you're using a third-party service such as Facebook Connect.--> |
| 8 | +첫번째 방법은 직접 관리하시는 데이터베이스나 리소스에 인증하고 있을때 유용하고, 두번째 방법은 페이스북같은 외부 인증을 사용할때 유용합니다. |
| 9 | + |
| 10 | +<!--Either way, the goal is the same: to update `req.session.userId` with the user's unique ID.--> |
| 11 | +어떤 방법이든 '`req.session.userId`에 사용자 고유의 ID를 넣고 싶다.'라는 목표는 같습니다. |
| 12 | + |
| 13 | +<!---### Authenticating over websockets--> |
| 14 | +### 웹소켓을 통한 인증 |
| 15 | + |
| 16 | +<!--This is the best choice if you're authenticating against an internal database or LDAP server, etc.--> |
| 17 | +내부의 데이터베이스 또는 LDAP서버 등 에 인증하고 있다면 이것이(웹소켓이) 최고의 선택일 거에요. |
| 18 | + |
| 19 | +```javascript |
| 20 | +// server/rpc/app.js |
| 21 | +exports.actions = function(req, res, ss){ |
| 22 | + |
| 23 | + // SocketStream에게 session 데이터를 로드하도록 명령 |
| 24 | + req.use('session'); |
| 25 | + |
| 26 | + return { |
| 27 | + authenticate: function(username, password){ |
| 28 | + |
| 29 | + // BD, LDAP, 등에서 사용자 확인 |
| 30 | + |
| 31 | + if (user) { |
| 32 | + req.session.setUserId(user.id); |
| 33 | + res(true); |
| 34 | + } else { |
| 35 | + res('Access denied!'); |
| 36 | + } |
| 37 | + |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +``` |
| 43 | +<!-- |
| 44 | + // tell SocketStream to load session data |
| 45 | + // lookup user in DB, LDAP, etc |
| 46 | +--> |
| 47 | +<!--Note: You could just set`req.session.userId` manually, but calling the `req.session.setUserId()` function saves the session and notifies SocketStream to immediately start sending events for this user (sent using `ss.publish.user()`) over the current websocket connection.--> |
| 48 | +주의: 당신이 `req.session.userId`을 그냥 수동으로 바꿀 수도 있지만, 그렇게 하기보단 `req.session.setUserId()`를 호출해 세션을 저장하고 현재의 웹소켓 연결을 사용하여 SocketStream에게 이 유저의(`ss.publish.user()`를 사용하여 전송) 이벤트 전송이 시작됬음을 알리는게 좋습니다. |
| 49 | + |
| 50 | +<!---### Authenticating using HTTP--> |
| 51 | +### HTTP이용한 인증 |
| 52 | + |
| 53 | +<!--Since the same session object is also available over HTTP you may easily authenticate a user by updating `req.session.userId` whilst processing a HTTP request.--> |
| 54 | +같은 세션 객체는 HTTP를 통하여도 가능하기 때문에 HTTP Request를 처리하는 동안 `req.session.userId`을 변경하여 인증할 수 있습니다. |
| 55 | + |
| 56 | +<!--Let's look at a very simple example by adding the following 'route' to `app.js`:--> |
| 57 | +다음에 나오는 'route'를 `app.js`에 추가한 매우 쉬운 예제를 봅시다: |
| 58 | + |
| 59 | +```javascript |
| 60 | +// app.js |
| 61 | +ss.http.router.on('/authenticateMe', function(req, res) { |
| 62 | + req.session.userId = '나솔'; |
| 63 | + req.session.save(function(err){ |
| 64 | + res.serve('main'); |
| 65 | + }); |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +<!--Next, add an RPC action which sends the contents of `req.session.userId` over the websocket:--> |
| 70 | +다음으로 웹소켓을 통해 `req.session.userId`의 컨텐츠들를 전송하는 RPC를 추가합니다. |
| 71 | + |
| 72 | +```javascript |
| 73 | +// server/rpc/app.js |
| 74 | +exports.actions = function(req, res, ss){ |
| 75 | + |
| 76 | + // SocketStream에게 session 데이터를 로드하게 명령 |
| 77 | + req.use('session'); |
| 78 | + |
| 79 | + return { |
| 80 | + |
| 81 | + getCurrentUser: function(){ |
| 82 | + res('현재 사용자는 ' + req.session.userId + '님 입니다.'); |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | +}; |
| 87 | +``` |
| 88 | +<!---// tell SocketStream to load session data--> |
| 89 | + |
| 90 | +<!--Now visit `http://localhost:3000/authenticateMe` then enter the following command in the browser's console:--> |
| 91 | +이제 `http://localhost:3000/authenticateMe` 가서 브라우저 콘솔에 다음 명령을 실행해보세요: |
| 92 | + |
| 93 | + ss.rpc('app.getCurrentUser') |
| 94 | + |
| 95 | +<!--And you'll see the following output:--> |
| 96 | +그러면 다음 결과가 출력 됩니다: |
| 97 | + |
| 98 | + 현재 사용자는 나솔님 입니다. |
| 99 | + |
| 100 | +<!---### Using Everyauth for Facebook Connect, Twitter, Github etc--> |
| 101 | +### Facebook Connect, Twitter, Github 등의 위한 Everyauth 사용 |
| 102 | + |
| 103 | +<!--SocketStream integrates well with popular authentication libraries such as [Everyauth](https://github.com/bnoguchi/everyauth).--> |
| 104 | +SocketStream은 [Everyauth](https://github.com/bnoguchi/everyauth)같은 널리쓰이는 인증 라이브러리들과 잘 통합됩니다. |
| 105 | + |
| 106 | +<!--Tip: Don't be tempted to follow the docs on the Everyauth website too closely - they are mainly geared at multi-page apps and/or specific to Express.--> |
| 107 | +팁: Everyauth 웹사이트의 문서를 너무 자세히 따르지 마세요 - 그것들은 주로 여러페이지를 가진 어플리케이션에 적합한 내용이거나 Express를 위한 겁니다. |
| 108 | + |
| 109 | +<!--Here's an example of a full app which authenticates against Twitter's OAuth service.--> |
| 110 | +트위터의 oAuth서비스에 대한 인증을 하는 전체 app의 예제입니다. |
| 111 | + |
| 112 | +<!--To get started, register your new app at https://dev.twitter.com/apps/new--> |
| 113 | +시작하기전에 https://dev.twitter.com/apps/new 에서 당신의 새 앱을 등록하세요. |
| 114 | + |
| 115 | +<!--When testing your app supply `http://127.0.0.1:3000` as the Callback URL. Change this to the real URL when your app goes into production.--> |
| 116 | +당신의 앱을 테스트 할때 콜백 URL로 `http://127.0.0.1:3000`이 제공됩니다. 당신을 앱을 제품으로 만들때 할때 이것을 실제 URL로 변경해야 합니다. |
| 117 | + |
| 118 | +```javascript |
| 119 | +// app.js |
| 120 | +var http = require('http') |
| 121 | + , ss = require('socketstream') |
| 122 | + , everyauth = require('everyauth'); |
| 123 | + |
| 124 | +ss.client.define('main', { |
| 125 | + view: 'app.jade', |
| 126 | + css: ['libs', 'app.styl'], |
| 127 | + code: ['libs', 'modules', 'main'] |
| 128 | +}); |
| 129 | + |
| 130 | +ss.http.router.on('/', function(req, res) { |
| 131 | + res.serve('main'); |
| 132 | +}); |
| 133 | + |
| 134 | +everyauth.twitter |
| 135 | + .consumerKey('여기에 CONSUMER ID 를 입력') |
| 136 | + .consumerSecret('여기에 CONSUMER SECRET 를 입력') |
| 137 | + .findOrCreateUser( function (session, accessToken, accessTokenSecret, twitterUserMetadata) { |
| 138 | + var userName = twitterUserMetadata.screen_name; |
| 139 | + console.log('트위터 유저이름: ', userName); |
| 140 | + session.userId = userName; |
| 141 | + session.save(); |
| 142 | + return true; |
| 143 | + }) |
| 144 | + .redirectPath('/'); |
| 145 | + |
| 146 | +ss.http.middleware.prepend(ss.http.connect.bodyParser()); |
| 147 | +ss.http.middleware.append(everyauth.middleware()); |
| 148 | + |
| 149 | +var server = http.Server(ss.http.middleware); |
| 150 | +server.listen(3000); |
| 151 | + |
| 152 | +ss.start(server); |
| 153 | + |
| 154 | +// http://local.host:3000/auth/twitter 에서 인증해볼수 있습니다. |
| 155 | +``` |
| 156 | +<!--// To authenticate visit http://local.host:3000/auth/twitter--> |
| 157 | + |
| 158 | +<!--Many more details on this and other examples coming soon.--> |
| 159 | +인증에 대한 더 상세한 설명과 예제들이 곧 나올꺼에요. |
0 commit comments