jhipster oauth2客户端秘密

And*_*unt 4 oauth-2.0 jhipster

我一直在试验jhipster.我已将我的应用配置为使用oauth2.为此,我在application.yml中有一个客户端密码

根据我在这个主题上发现的几篇文章,客户秘密应该始终保密.例如,请检查https://aaronparecki.com/articles/2012/07/29/1/oauth2-simplified

客户机密必须保密.如果部署的应用程序无法保密,例如Javascript或本机应用程序,则不使用该机密.

我注意到生成的auth.oauth2.service.js包含纯文本的秘密:

        return {
            login: function(credentials) {
                var data = "username=" + credentials.username + "&password="
                    + credentials.password + "&grant_type=password&scope=read%20write&" +
                    "client_secret=mySecretOAuthSecret&client_id=myapp";
                return $http.post('oauth/token', data, {
                    headers: {
                        "Content-Type": "application/x-www-form-urlencoded",
                        "Accept": "application/json",
                        "Authorization": "Basic " + Base64.encode("myapp" + ':' + "mySecretOAuthSecret")
                    }
                }).success(function (response) {
                    var expiredAt = new Date();
                    expiredAt.setSeconds(expiredAt.getSeconds() + response.expires_in);
                    response.expires_at = expiredAt.getTime();
                    localStorageService.set('token', response);
                    return response;
                });
            },
Run Code Online (Sandbox Code Playgroud)

我知道在缩小的javascript中找到它会有点困难,但是任何寻找'client_secret'的人都会很快获得奖励.

我错过了什么吗?或者jHipster oauth实施是不安全的?

谢谢,安迪

sdo*_*see 9

由于像jhipster这样的JS客户端无法保密客户机密"秘密",因此根本不使用客户机秘密.jhipster使用的OAuth2资源所有者密码凭据授权流程适用于非常受信任的客户端 - jhipster的客户端.它允许您跳过正常的"授权"端点并直接转到"令牌"端点,以使用您的用户凭据获取令牌.如果您的Spring授权服务器(AS)定义了客户端密钥,则需要从客户端JS传递该秘密.但是,如果从AS中的内存客户端设置中删除秘密定义(例如,在OAuth2ServerConfiguration.java中注释掉该行),则可以在JS中完全删除它(参见下文)

return {
   login: function(credentials) {
      var data = "username=" + credentials.username + "&password=" + credentials.password + "&grant_type=password&scope=read%20write&";
      return $http.post('oauth/token', data, {
         headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Accept": "application/json",
            "Authorization": "Basic " + Base64.encode("myapp" + ':' + "")
         }
      }).success(function (response) {
         var expiredAt = new Date();
         expiredAt.setSeconds(expiredAt.getSeconds() + response.expires_in);
         response.expires_at = expiredAt.getTime();
         localStorageService.set('token', response);
         return response;
      });
   },
Run Code Online (Sandbox Code Playgroud)

删除你的客户端秘密后,我不认为你的应用程序真的更安全但感觉更清洁和诚实 - 因为你承认使用仅限JS的客户端,你只能这么安全.对于JS和本机客户端,通常使用隐式流,并且它不会烦扰客户机密钥.它从更强大的授权代码授权流程中简化,因为JS或本机客户端无法保密.

无论如何,jhipster可能不应该在JS源代码中拥有客户端秘密,但我不相信它会造成任何伤害(因为唯一的选择是拥有一个空白的客户端秘密,这不是更安全).你不是不安全的(因为规范允许这样的事情)但你使用授权代码流更安全(这需要在jhipster实现中做一些工作)或者让轻型服务器代理添加客户端 - 对"令牌"端点的请求是秘密而不是直接来自JS.服务器到服务器的通信(例如,通过代理)使秘密远离浏览器的视图.

看到这篇文章,讨论了一个只有JS的客户端与oauth2的陷阱:http://alexbilbie.com/2014/11/oauth-and-javascript/

这是一个使用oauth2和angularjs并在代理上使用spring的示例:https://spring.io/blog/2015/02/03/sso-with-oauth2-angular-js-and-spring-security-part-v