Explorar el Código

finish OIDC provider

Starink hace 6 años
padre
commit
2fca938561
Se han modificado 5 ficheros con 20 adiciones y 17 borrados
  1. 4 2
      middleware/auth.js
  2. 5 3
      pages/login.vue
  3. 1 1
      server/config/secret.json
  4. 7 10
      server/controller/user.js
  5. 3 1
      server/index.js

+ 4 - 2
middleware/auth.js

@@ -18,13 +18,15 @@ export default function ({route, store, req, res, redirect}) {
   }
 
   console.log("path:"+path);
+  let mainPath = path.split("?")[0];
   store.commit('saveOriginPath', path);
 
-  if (!token && path !== "/" && path !=="/login" && path !== "/register") {
+  if (!token && mainPath !== "/" && mainPath !=="/login" && mainPath !== "/register" && mainPath !=="/error" && mainPath !== "/api/receiveIDToken") {
     let return_uri = encodeURIComponent("http://localhost:3000/receiveIDToken");
+    store.commit('saveOriginPath', path);
     console.log("encoded path");
     let url = 'http://localhost:3000/api/interaction?'+
-      'client_id='+ 'seec-portal-1919810-114514' +
+      'client_id='+ 'seec-portal' +
       '&scope=openid' +
       '&redirect_uri=' + return_uri +
       '&response_type=code';

+ 5 - 3
pages/login.vue

@@ -148,13 +148,15 @@
                 localStorage.removeItem("password");
               }
 
-              this.$store.commit("saveUser", res.data.user);
-              this.$store.commit("saveToken", res.data.token);
+              // this.$store.commit("saveUser", res.data.user);
+              // this.$store.commit("saveToken", res.data.token);
               this.$message({
                 showClose: true,
-                message: "欢迎回来," + res.data.user.name,
+                message: "欢迎回来," + res.data.name,
                 type: 'success'
               });
+              window.location.href = res.data.path + "?id_token=" + res.data.token;
+
             }else{
               this.$message({
                 showClose: true,

+ 1 - 1
server/config/secret.json

@@ -1,3 +1,3 @@
 {
-  "sign": "seec-1919810-OIDC-114514"
+  "sign": "SEEC-1919810-OIDC-114514"
 }

+ 7 - 10
server/controller/user.js

@@ -24,21 +24,18 @@ class UserController {
               let redirect_uri = ctx.cookies.get('redirect_uri');
 
               let data = {
-                iss : 'http://'+ process.env.HOST,
-                sub: encodeURIComponent("uid=" + user.id + "&name=" + uid.name + "&phone=" + user.phone),
+                iss : 'http://'+ (process.env.HOST || "localhost"),
+                sub: encodeURIComponent(user.id + "&" + user.name + "&" + user.phone),
                 aud: ctx.cookies.get('client_id'),
-                auth_time: new Date().getTime() / 1000,
-                iat: new Date().getTime() / 1000,
-                exp: new Date().getTime() / 1000 + 24 * 60 * 60,
+                auth_time: Math.floor(new Date().getTime() / 1000),
+                iat: Math.floor(new Date().getTime() / 1000),
+                exp: Math.floor(new Date().getTime() / 1000 + 24 * 60 * 60),
               };
               const token = jwt.sign(data, secret.sign);
 
-              ctx.set('Content-Type', 'application/x-www-form-urlencoded');
-              ctx.redirect(redirect_uri + "?id_token=" + token);
+              //ctx.redirect(redirect_uri + "?id_token=" + token);
 
-                // ctx.body = Response.success(
-                //     {user: {phone: user.phone, id: user.id, name: user.name},
-                //         token: token});
+              ctx.body = Response.success({token: token, name: user.nam, path:redirect_uri});
             }
         } else {
             ctx.body = Response.failed('此账号不存在');

+ 3 - 1
server/index.js

@@ -31,7 +31,9 @@ app.use(bodyparser({
 }));
 
 app.use(cors({
-    origin:  'http://localhost',
+  origin:  (ctx)=>{
+    return "*";
+  },
     // exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
     maxAge: 3600,
     credentials: true,