Meteor.jsでカスタムトークンサーバーを介して認証を行う方法は?
構成パラメーターとしてトークン エンドポイント、クライアント ID、シークレット、およびスコープを取得するだけで認証を処理するカスタム トークン サーバー用の accounts-google のようなパッケージはありますか。
Meteor.jsでカスタムトークンサーバーを介して認証を行う方法は?
構成パラメーターとしてトークン エンドポイント、クライアント ID、シークレット、およびスコープを取得するだけで認証を処理するカスタム トークン サーバー用の accounts-google のようなパッケージはありますか。
一般的な oauth パッケージについては知りません。しかし、特定のサーバー用のパッケージを作成するのはそれほど難しくありません。見るべき例がたくさんあるからです。
例として accounts-github を使用して、クライアントで接続を確立するためのコードを次に示します。エンドポイント URL、クライアント ID、スコープなどに注意してください。これでポップアップが処理されますが、カスタム CSS を含めることをお勧めします。
var loginUrl =
'https://github.com/login/oauth/authorize' +
'?client_id=' + config.clientId +
'&scope=' + flatScope +
'&redirect_uri=' + OAuth._redirectUri('github', config) +
'&state=' + OAuth._stateParam(loginStyle, credentialToken);
OAuth.launchLogin({
loginService: "github",
loginStyle: loginStyle,
loginUrl: loginUrl,
credentialRequestCompleteCallback: credentialRequestCompleteCallback,
credentialToken: credentialToken,
popupOptions: {width: 900, height: 450}
});
そして、これはサーバー側からのスニペットで、アクセス トークンを取得するプロセスを完了しています。
var getAccessToken = function (query) {
var config = ServiceConfiguration.configurations.findOne({service: 'github'});
if (!config)
throw new ServiceConfiguration.ConfigError();
var response;
try {
response = HTTP.post(
"https://github.com/login/oauth/access_token", {
headers: {
Accept: 'application/json',
"User-Agent": userAgent
},
params: {
code: query.code,
client_id: config.clientId,
client_secret: OAuth.openSecret(config.secret),
redirect_uri: OAuth._redirectUri('github', config),
state: query.state
}
});
} catch (err) {
throw _.extend(new Error("Failed to complete OAuth handshake with Github. " + err.message),
{response: err.response});
}
if (response.data.error) { // if the http response was a json object with an error attribute
throw new Error("Failed to complete OAuth handshake with GitHub. " + response.data.error);
} else {
return response.data.access_token;
}
};
そして、トークンを利用してユーザー ID を取得します。
var getIdentity = function (accessToken) {
try {
return HTTP.get(
"https://api.github.com/user", {
headers: {"User-Agent": userAgent}, // http://developer.github.com/v3/#user-agent-required
params: {access_token: accessToken}
}).data;
} catch (err) {
throw _.extend(new Error("Failed to fetch identity from Github. " + err.message),
{response: err.response});
}
};
githubおよびaccounts-githubパッケージは、リファレンスとして非常に役立つはずです。