Git Product home page Git Product logo

oauth2-encryption-demo's Introduction

易路开放平台oauth 加解密说明

作者:易路软件前端团队,sakura,sheely

接口:api/oauth2/oauth2_token

功能:获取token和linkid

参数:appid

将参数填入headers中identification字段,并且对参数进行base64编码

const $ = require('jquery');
const base64 = require('base64-url');

// 原始参数
let appid = JSON.stringify({"appid" : "$appid"});

// base64编码
let base64Appid = base64.encode(appid);

// 写入headers并发起请求
$.ajax({
    url: '/api/oauth2/oauth2_token',
    type: 'POST',
    dataType: 'json',
    headers: {
        'identification': base64Appid
    },
    success: function (data) {
        console.log(data)
    },
    error: function (data) {
        console.log(data)
    }
});

返回值:

{
  "code": 0,
  "msg": "OK",
  "data": {
    "token": "token",
    "linkid": "linkid"
  }
}

加密数据

加解密模块 eroad-oauth2-encryption

const cipher = require('eroad-oauth2-encryption');

// 要发送的数据
let data = {
    "code": 0,
    "msg": "OK",
    "data": {
        "name": "测试数据"
    }
};
// api/oauth2/oauth2_token接口返回值中的token
let token = '$token'; 
let appid = '$appid';

// 对数据进行加密
let enData = cipher.encrypt(data, token, appid);

// 要解密的数据
let rdata = {
    "summary": "c287350f8cebd36ca26b343d8f6bcaae",
    "data": "s+wrANjtWJf5vemG4zHufv+vKRzxOFI75fp6wEG7CfdNxvjzIA/L2iYpOLW9F/eYZJ/5T7SEoDwWGL+zdGugY2hVZpziTdc7hFlFzuWvlTafbgrb/JwZOX+Iw7i2IcIY"
},
// 对数据进行解密
let deData = cipher.decrypt(rdata, token, appid)

接口:api/oauth2/decrypt_test

功能:加解密测试接口, 返回你传入的参数

参数:

  • headers中的identification填入api/oauth2/oauth2_token接口返回的linkid
  • body中放入已经加密过的数据
// linkid
let linkid = JSON.stringify({"linkid" : "673fe871-defb-416e-a995-bf29644ecc97"});
let base64Linkid = base64.encode(linkid);

// 要发送的数据
let data = {
    "code": 0,
    "msg": "OK",
    "data": {
        "name": "测试数据"
    }
};
// api/oauth2/oauth2_token接口返回值中的token
let token = '$token'; 
let appid = '$appid';

// 对数据进行加密
let enData = cipher.encrypt(data, token, appid);

// 传入加密后数据
$.ajax({
    url: '/api/oauth2/oauth2_token',
    type: 'POST',
    dataType: 'json',
    data: enData,
    headers: {
        'identification': base64Linkid
    },
    success: function (res) {
	    // 返回的数据是加密后的,需要进行解密
        console.log(res)
        let deData = cipher.decrypt(res, token, appid)
        console.log(deData)
    },
    error: function (data) {
        console.log(data)
    }
});

oauth2-encryption-demo's People

Contributors

eroad-f2e avatar sheely avatar

Stargazers

 avatar  avatar

Forkers

magicxxx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.