项目作者: abapChaoLiu

项目描述 :
ABAP JWT Generator
高级语言: ABAP
项目地址: git://github.com/abapChaoLiu/abap_jwt_generator.git
创建时间: 2020-05-09T02:11:00Z
项目社区:https://github.com/abapChaoLiu/abap_jwt_generator

开源协议:MIT License

下载


abapLint

ABAP JWT Generator

Generate Json Web Token (JWT) in ABAP.

Demo 1 - method get_jwt_by_profile().

If JWT profile is maintained in table ZJWT_PROFILE, use method get_jwt_by_profile() to derive JWT token by profile name.

JWT profile screenshot

  1. DATA: jwt_generator TYPE REF TO zcl_jwt_generator,
  2. jwt TYPE string.
  3. CREATE OBJECT jwt_client.
  4. jwt = jwt_client->get_jwt_by_profile( 'JWT_PROFILE_NAME' ).

Demo 2 - method generate_jwt().

If JWT profile is not maintained, use method generate_jwt() to derive JWT token.

  1. REPORT zrpt_jwt_generator_demo.
  2. DATA: jwt_generator TYPE REF TO zcl_jwt_generator.
  3. DATA: jwt_header TYPE zcl_jwt_generator=>ty_jwt_header,
  4. jwt_claim TYPE zcl_jwt_generator=>ty_jwt_claim.
  5. DATA: exp_second TYPE int8,
  6. ssf_info TYPE ssfinfo.
  7. DATA: start_timestamp TYPE timestamp VALUE '19700101000000',
  8. ssf_id TYPE ssfid VALUE '<implicit>',
  9. "PSE profile with private key
  10. ssf_profile TYPE ssfprof VALUE 'SAPZJWTSF001.pse'.
  11. jwt_header-alg = 'RS256'.
  12. jwt_claim = VALUE #( iss = 'UserID'
  13. sub = 'example@gmail.com'
  14. aud = 'https://login.example.com'
  15. exp = exp_second ).
  16. ssf_info = VALUE #( id = ssf_id profile = ssf_profile ).
  17. CREATE OBJECT jwt_generator.
  18. TRY.
  19. jwt_generator->generate_jwt(
  20. EXPORTING
  21. jwt_header = jwt_header
  22. jwt_claim = jwt_claim
  23. ssf_info = ssfinfo
  24. RECEIVING
  25. jwt = DATA(jwt) ).
  26. CATCH zcx_jwt_generator INTO DATA(lo_exp).
  27. WRITE /: 'ERROR when generate JWT token.'.
  28. WRITE /: lo_exp->get_text( ).
  29. ENDTRY.

Demo 3 - method get_access_token_by_profile().

If JWT profile is maintained in table ZJWT_PROFILE, use method get_access_token_by_profile() to get JWT Access Token.

JWT profile screenshot

  1. DATA: jwt_generator TYPE REF TO zcl_jwt_generator,
  2. jwt_access_token TYPE string.
  3. CREATE OBJECT jwt_client.
  4. jwt_access_token = jwt_client->get_access_token_by_profile( 'JWT_PROFILE_NAME' ).

Demo 4 - method generate_jwt_with_secret().

You can use method generate_jwt_with_secret() to generate a JWT with the secret key, like JWT Debugger.

  1. GET TIME STAMP FIELD DATA(timestamp).
  2. DATA(jwt_generator) = NEW zcl_jwt_generator( ).
  3. TRY.
  4. DATA(jwt_result) =
  5. jwt_generator->generate_jwt_with_secret(
  6. jwt_header = VALUE #( alg = 'HS256' typ = 'JWT' )
  7. jwt_claim = VALUE #( sub = '1234567890'
  8. name = 'John Doe'
  9. iat = jwt_generator->convert_abap_timestamp_to_unix( timestamp ) )
  10. secret = '1WRAv0Usf90-jr2W7UQBQBLvIBBFq8vumq-VzrR3h7E'
  11. algorithm = 'SHA256' ).
  12. CATCH zcx_jwt_generator INTO DATA(lx_jwt).
  13. ENDTRY.

Credits and references

Class ZCL_JWT_Generator is modified from Dimitri Seifmann’s post Connect from AS ABAP to Google Cloud Platform App-Engine resource secured with Google Identity-Aware Proxy.