Search in sources :

Example 56 with JWT

use of com.auth0.android.jwt.JWT in project micro-service-examples by jetlinks.

the class JwtAuthSupplier method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    String token = request.getHeader(HttpHeaders.AUTHORIZATION);
    if (StringUtils.isEmpty(token) || !token.startsWith("jwt")) {
        return true;
    }
    JWTVerifier verifier = jwt.createVerifier();
    DecodedJWT jwt = verifier.verify(token.substring(4));
    ContextUtils.currentContext().put(Authentication.class, factory.create().json(jwt.getSubject()).build());
    return true;
}
Also used : JWTVerifier(com.auth0.jwt.interfaces.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 57 with JWT

use of com.auth0.android.jwt.JWT in project TCSS450-Mobile-App by TCSS450-Team7-MobileApp.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mMainActivity = this;
    MainActivityArgs args = MainActivityArgs.fromBundle(getIntent().getExtras());
    // Import com.auth0.android.jwt.JWT
    JWT jwt = new JWT(args.getJwt());
    // created on the web service.
    if (!jwt.isExpired(0)) {
        jwt = new JWT(args.getJwt());
    }
    new ViewModelProvider(this, new UserInfoViewModel.UserInfoViewModelFactory(args.getEmail(), jwt.toString(), args.getFirst(), args.getLast(), args.getNick(), args.getId())).get(UserInfoViewModel.class);
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    // Make sure the new statements go BELOW setContentView
    BottomNavigationView navView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_message, R.id.navigation_home, R.id.navigation_weather).build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navView, navController);
    mNewMessageModel = new ViewModelProvider(this).get(NewMessageCountViewModel.class);
    navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
        if (destination.getId() == R.id.navigation_message) {
            // When the user navigates to the chats page, reset the new message count.
            // This will need some extra logic for your project as it should have
            // multiple chat rooms.
            mNewMessageModel.reset();
        }
    });
    mNewMessageModel.addMessageCountObserver(this, count -> {
        BadgeDrawable badge = binding.navView.getOrCreateBadge(R.id.navigation_message);
        badge.setMaxCharacterCount(2);
        if (count > 0) {
            // new messages! update and show the notification badge.
            badge.setNumber(count);
            badge.setVisible(true);
        } else {
            // user did some action to clear the new messages, remove the badge
            badge.clearNumber();
            badge.setVisible(false);
        }
    });
}
Also used : BadgeDrawable(com.google.android.material.badge.BadgeDrawable) NewMessageCountViewModel(edu.uw.tcss450.blynch99.tcss450mobileapp.model.NewMessageCountViewModel) JWT(com.auth0.android.jwt.JWT) BottomNavigationView(com.google.android.material.bottomnavigation.BottomNavigationView) NavController(androidx.navigation.NavController) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 58 with JWT

use of com.auth0.android.jwt.JWT in project bitrxc-server by bitrxc.

the class TokenManager method createTokenForAdmin.

// 管理员端token生成
public String createTokenForAdmin(String phone) {
    Date expirationTime = new Date(System.currentTimeMillis() + this.tokenExpiration);
    // 私钥及加密算法
    Algorithm algorithm = Algorithm.HMAC256(tokenSignKey);
    // 设置头信息
    Map<String, Object> header = new HashMap<>();
    header.put("typ", "JWT");
    header.put("alg", "HS256");
    return JWT.create().withHeader(header).withExpiresAt(expirationTime).withClaim("phone", phone).sign(algorithm);
}
Also used : HashMap(java.util.HashMap) Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date)

Example 59 with JWT

use of com.auth0.android.jwt.JWT in project tutorials by jhkim105.

the class JwtAuthenticationTokenService method parseToken.

public AuthUser parseToken(String token) {
    checkToken(token);
    try {
        DecodedJWT jwt = JWT.decode(token);
        String id = jwt.getClaim("id").asString();
        String username = jwt.getClaim("username").asString();
        String authority = jwt.getClaim("authority").asString();
        AuthUser authUser = AuthUser.builder().id(id).username(username).authority(authority).build();
        return authUser;
    } catch (JWTDecodeException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : JWTDecodeException(com.auth0.jwt.exceptions.JWTDecodeException) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 60 with JWT

use of com.auth0.android.jwt.JWT in project study by bage2014.

the class JWTTest method main.

public static void main(String[] args) throws IllegalArgumentException, UnsupportedEncodingException {
    // jjwt();
    String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
    try {
        Algorithm algorithm = Algorithm.HMAC256("your-256-bit-secret");
        // Algorithm algorithm = Algorithm.HMAC256("your-256-bit-secret".getBytes("UTF-8"));
        JWTVerifier verifier = JWT.require(algorithm).build();
        DecodedJWT jwt = verifier.verify(token);
        System.out.println(jwt);
    } catch (JWTVerificationException exception) {
        // Invalid signature/claims
        exception.printStackTrace();
    }
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) Algorithm(com.auth0.jwt.algorithms.Algorithm) SignatureAlgorithm(io.jsonwebtoken.SignatureAlgorithm) JWTVerifier(com.auth0.jwt.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Aggregations

DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)305 Test (org.junit.Test)217 Algorithm (com.auth0.jwt.algorithms.Algorithm)110 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)82 JWTVerifier (com.auth0.jwt.JWTVerifier)79 IOException (java.io.IOException)60 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)54 ECDSAAlgorithmTest (com.auth0.jwt.algorithms.ECDSAAlgorithmTest)53 Date (java.util.Date)50 Claim (com.auth0.jwt.interfaces.Claim)36 RSAPublicKey (java.security.interfaces.RSAPublicKey)34 ECPublicKey (java.security.interfaces.ECPublicKey)27 ECDSAKeyProvider (com.auth0.jwt.interfaces.ECDSAKeyProvider)26 HashMap (java.util.HashMap)25 JWTDecodeException (com.auth0.jwt.exceptions.JWTDecodeException)20 Instant (java.time.Instant)20 JsonObject (com.google.gson.JsonObject)19 ServletException (javax.servlet.ServletException)19 JWT (com.auth0.jwt.JWT)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)18