@Override protected User createAdminUser() { User user = super.createAdminUser(); user.setName(ADMIN_NAME); return user; }
/** * Changes password */ @PostMapping("/users/{id}/password") @ResponseStatus(HttpStatus.NO_CONTENT) public Mono<Void> changePassword(@PathVariable ID id, @RequestBody @Valid Mono<ChangePasswordForm> changePasswordForm, ServerHttpResponse response) { log.debug("Changing password ... "); return userWithToken(lemonReactiveService.changePassword(id, changePasswordForm), response).then(); }
/** * Fetch a self-sufficient token with embedded UserDto - for interservice communications */ @GetMapping("/fetch-full-token") public Mono<Map<String, String>> fetchFullToken(@RequestHeader(HttpHeaders.AUTHORIZATION) String authHeader) { log.debug("Fetching a micro token"); return lemonReactiveService.fetchFullToken(authHeader); }
@Override protected void updateUserFields(User user, User updatedUser, UserDto currentUser) { super.updateUserFields(user, updatedUser, currentUser); user.setName(updatedUser.getName()); }
/** * Returns the context data to be sent to the client, * i.e. <code>reCaptchaSiteKey</code> and all the properties * prefixed with <code>lemon.shared</code>. * * To send custom properties, put those in your application * properties in the format <em>lemon.shared.fooBar</em>. * * If a user is logged in, it also returns the user data * and a new authorization token. If expirationMillis is not provided, * the expiration of the new token is set to the default. * * Override this method if needed. */ public Mono<Map<String, Object>> getContext(Optional<Long> expirationMillis, ServerHttpResponse response) { log.debug("Getting context ..."); Mono<Optional<UserDto>> userDtoMono = LecrUtils.currentUser(); return userDtoMono.map(optionalUser -> { Map<String, Object> context = buildContext(); optionalUser.ifPresent(user -> { addAuthHeader(response, user, expirationMillis.orElse(properties.getJwt().getExpirationMillis())); context.put("user", user); }); return context; }); }
/** * Afgter a successful login, returns the current user with an authorization header. */ @PostMapping("/login") public Mono<UserDto> login(ServerWebExchange exchange) { log.debug("Returning current user ... "); return ReactiveSecurityContextHolder.getContext() .map(SecurityContext::getAuthentication) .map(Authentication::getPrincipal) .cast(LemonPrincipal.class) .doOnNext(LemonPrincipal::eraseCredentials) .map(LemonPrincipal::currentUser) .zipWith(exchange.getFormData()) .doOnNext(tuple -> { long expirationMillis = lemonReactiveService.getExpirationMillis(tuple.getT2()); lemonReactiveService.addAuthHeader(exchange.getResponse(), tuple.getT1(), expirationMillis); }) .map(Tuple2::getT1); }
/** * Fetch a new token - for session sliding, switch user etc. */ @PostMapping("/fetch-new-auth-token") public Mono<Map<String, String>> fetchNewToken(ServerWebExchange exchange) { log.debug("Fetching a new token ... "); return lemonReactiveService.fetchNewToken(exchange); //return LecUtils.mapOf("token", lemonService.fetchNewToken(expirationMillis, username)); }
/** * Fetches a user by ID */ @GetMapping("/users/{id}") public Mono<U> fetchUserById(@PathVariable ID id) { log.debug("Fetching user: " + id); return lemonReactiveService.fetchUserById(id); }
/** * Fetches a user by email */ @PostMapping("/users/fetch-by-email") public Mono<U> fetchUserByEmail(ServerWebExchange exchange) { log.debug("Fetching user by email ... "); return lemonReactiveService.fetchUserByEmail(exchange.getFormData()); }
/** * returns the current user and a new authorization token in the response */ public Mono<UserDto> userWithToken(Mono<UserDto> userDto, ServerHttpResponse response, long expirationMillis) { return userDto.doOnNext(user -> { log.debug("Adding auth header for " + user.getUsername()); addAuthHeader(response, user, expirationMillis); }); }
/** * Changes the email */ @PostMapping("/users/{userId}/email") public Mono<UserDto> changeEmail( @PathVariable ID userId, ServerWebExchange exchange) { log.debug("Changing email of user ..."); return userWithToken(lemonReactiveService.changeEmail( userId, exchange.getFormData()), exchange.getResponse()); }
protected U updateUser(U user, Optional<UserDto> currentUser, String patch) { log.debug("Updating user: " + user); U updatedUser = LecrUtils.applyPatch(user, patch); // create a patched form LexUtils.validateBean("updatedUser", updatedUser, UserUtils.UpdateValidation.class).go(); LecmUtils.ensureCorrectVersion(user, updatedUser); updateUserFields(user, updatedUser, currentUser.get()); log.debug("Updated user: " + user); return user; }
public void onStartup() { userDetailsService .findByUsername(properties.getAdmin().getUsername()) // Check if the user already exists .doOnError(e -> e instanceof UsernameNotFoundException, e -> { // Doesn't exist. So, create it. log.debug("Creating first admin ... "); U user = createAdminUser(); userRepository.insert(user).doOnError(err -> { log.warn("Error creating initial admin " + err); }).subscribe(); log.debug("Created first admin."); }).subscribe(); }