auth.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const Response = require('../util/response');
  2. const jwt = require('jsonwebtoken');
  3. class AuthController{
  4. static processIDToken(ctx){
  5. const query = ctx.query;
  6. const {id_token} = query;
  7. if(!id_token){
  8. ctx.body = Response.failed("非法请求!")
  9. }else{
  10. jwt.verify(id_token, 'SEEC-1919810-OIDC-114514', (err, authData) => {
  11. if(err) {
  12. console.log("err:", err);
  13. ctx.redirect("/error");
  14. }
  15. else {
  16. const { iss, sub, aud, auth_time, iat, exp} = authData;
  17. if(!iss || !sub || !aud || !auth_time || !iat || !exp){
  18. ctx.redirect("/error");
  19. }
  20. else if(iss !== "http://localhost" || aud!=='seec-portal' ){
  21. ctx.redirect("/error");
  22. }
  23. else{
  24. let claims = sub.split('%26');
  25. let origin_path = ctx.cookies.get('origin_path');
  26. let user = {
  27. id: parseInt(claims[0]),
  28. name: claims[1],
  29. phone: claims[2]
  30. };
  31. let my_token = jwt.sign(
  32. user,'seec-portal',{ expiresIn: '1day' }
  33. );
  34. // ctx.res.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
  35. // ctx.res.setHeader("Access-Control-Allow-Credentials", true);
  36. // ctx.cookies.user = user;
  37. // ctx.cookies.token = my_token;
  38. //
  39. // console.log("origin_path", origin_path)
  40. //
  41. // ctx.redirect(origin_path);
  42. let url = '/setToken?origin_path=' + origin_path + '&token=' + my_token + '&id=' + user.id + '&name=' + user.name + '&phone=' + user.phone;
  43. // store.commit("saveUser", user);
  44. // store.commit("saveToken", my_token);
  45. ctx.redirect(url);
  46. }
  47. }
  48. });
  49. }
  50. }
  51. static authenticate(ctx){
  52. const query = ctx.query;
  53. const {path} = query;
  54. let return_uri = encodeURIComponent("http://localhost:8000/api/receiveIDToken");
  55. let url = 'http://localhost:3000/api/interaction?'+
  56. 'client_id='+ 'seec-portal' +
  57. '&scope=openid' +
  58. '&redirect_uri=' + return_uri +
  59. '&response_type=code';
  60. // ctx.cookies.set("origin_path", path);
  61. console.log("redirect to " + url);
  62. return ctx.redirect(url);
  63. }
  64. }
  65. module.exports = AuthController;