Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Adding special information for function invoking by backend triggers. #1115

Open
1 of 2 tasks
Zing22 opened this issue May 6, 2023 · 1 comment
Open
1 of 2 tasks
Labels
feature New feature or request

Comments

@Zing22
Copy link

Zing22 commented May 6, 2023

Search before asking

  • I had searched in the issues and found no similar issues.

Feature Request

I have a __interceptor__ function to check signature from front-end (shown below). But when the triggers invoke function from back-end, it will be intercepted because of no signature.

i hope to have special mark for the invoking by triggers, or just skip __interceptor__ like cloud.invoke() does.

Here is the code of my __interceptor__ function, it will print "invalid sign" when triggers calling.

import cloud from '@lafjs/cloud'
import CryptoJS from 'crypto-js';


// 检查签名是否有效
function verifySign(signKey: string, data: string, signature: string) {

  // 使用HMAC-SHA256算法进行验证
  const isValid = CryptoJS.HmacSHA256(data, signKey).toString() === signature;
  return isValid;
}


function compareDateStrings(dateString1: string, dateString2: string, n: number): boolean {
  const date1 = new Date(dateString1);
  const date2 = new Date(dateString2);
  const diffInSeconds = Math.abs((date1.getTime() - date2.getTime()) / 1000);
  return diffInSeconds <= n;
}


export async function main(ctx: FunctionContext) {
  let signKey = cloud.env.SIGN_KEY;
  if (!signKey) {
    // 没有开启
    return true;
  }
  const { signdata, signstr } = ctx.headers;
  const isValid = verifySign(signKey, signdata, signstr);

  if (!isValid) {
    console.log("invalid sign");
    return false;
  }

  // 检查时间
  const now = new Date().toISOString();
  const threshold = 30;  // 秒
  const timeCheck = compareDateStrings(now, signdata, threshold);

  if (!timeCheck) {
    console.log("time check failed", now, signdata);
    return false;
  }

  return true;
}

Are you willing to submit PR?

  • Yes I am willing to submit a PR!
@Zing22 Zing22 added the feature New feature or request label May 6, 2023
@HUAHUAI23
Copy link
Contributor

you can use cloud.share save global information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants