// Path: core/src/main/java/org/juiser/model/User.java
// public interface User {
// 
//     /**
//      * Returns {@code true} if the user has been authenticated (proven their identity) or {@code false} if the user
//      * is considered anonymous.
//      *
//      * @return {@code true} if the user has been authenticated (proven their identity) or {@code false} if the user
//      * is considered anonymous.
//      */
//     boolean isAuthenticated();
// 
//     /**
//      * Returns {@code true} if the user identity is unknown, {@code false} if the user is authenticated.
//      *
//      * @return {@code true} if the user identity is unknown, {@code false} if the user is authenticated.
//      */
//     boolean isAnonymous();
// 
//     String getHref();
// 
//     String getId();
// 
//     String getName(); //full name
// 
//     String getGivenName(); //aka 'first name' in Western cultures
// 
//     String getFamilyName(); //aka surname
// 
//     String getMiddleName();
// 
//     String getNickname(); //aka 'Mike' if givenName is 'Michael'
// 
//     String getUsername(); //OIDC 'preferred_username'
// 
//     URL getProfile();
// 
//     URL getPicture();
// 
//     URL getWebsite();
// 
//     String getEmail();
// 
//     boolean isEmailVerified();
// 
//     String getGender();
// 
//     LocalDate getBirthdate();
// 
//     TimeZone getZoneInfo();
// 
//     Locale getLocale();
// 
//     Phone getPhone();
// 
//     String getPhoneNumber();
// 
//     boolean isPhoneNumberVerified();
// 
//     ZonedDateTime getCreatedAt();
// 
//     ZonedDateTime getUpdatedAt();
// }
// 
// Path: spring/spring-security/src/main/java/org/juiser/spring/security/core/userdetails/ForwardedUserDetails.java
// public class ForwardedUserDetails implements UserDetails {
// 
//     private final User user;
//     private final Collection<? extends GrantedAuthority> grantedAuthorities;
// 
//     public ForwardedUserDetails(User user, Collection<? extends GrantedAuthority> grantedAuthorities) {
//         Assert.notNull(user, "User argument cannot be null.");
//         Assert.hasText(user.getUsername(), "User username cannot be null or empty.");
//         this.user = user;
//         if (CollectionUtils.isEmpty(grantedAuthorities)) {
//             this.grantedAuthorities = java.util.Collections.emptyList();
//         } else {
//             this.grantedAuthorities = java.util.Collections.unmodifiableCollection(grantedAuthorities);
//         }
//     }
// 
//     public User getUser() {
//         return this.user;
//     }
// 
//     @Override
//     public Collection<? extends GrantedAuthority> getAuthorities() {
//         return this.grantedAuthorities;
//     }
// 
//     @Override
//     public String getPassword() {
//         return null;
//     }
// 
//     @Override
//     public String getUsername() {
//         return getUser().getUsername();
//     }
// 
//     @Override
//     public boolean isAccountNonExpired() {
//         return true; //gateway will not forward expired accounts
//     }
// 
//     @Override
//     public boolean isAccountNonLocked() {
//         return true; //gateway will not forward locked accounts
//     }
// 
//     @Override
//     public boolean isCredentialsNonExpired() {
//         return true; //gateway will not forward expired accounts
//     }
// 
//     @Override
//     public boolean isEnabled() {
//         return true; //gateway will not forward disabled accounts
//     }
// }


import org.juiser.model.User;
import org.juiser.spring.security.core.userdetails.ForwardedUserDetails;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.lang.Assert;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Function;
/*
 * Copyright 2017 Les Hazlewood and the respective Juiser contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.juiser.spring.security.authentication;



/**
 * @since 1.0.0
 */
public class JwsToUserDetailsConverter implements Function<String, UserDetails> {

    private final Function<String, Claims> claimsExtractor;
    private final Function<Claims, User> claimsUserFactory;
    private final Function<Claims, Collection<? extends GrantedAuthority>> authoritiesResolver;

    public JwsToUserDetailsConverter(Function<String, Claims> claimsExtractor,
                                     Function<Claims, User> claimsUserFactory,
                                     Function<Claims, Collection<? extends GrantedAuthority>> authoritiesResolver) {
        Assert.notNull(claimsExtractor, "claimsExtractor cannot be null.");
        Assert.notNull(claimsUserFactory, "claimsUserFactory cannot be null.");
        this.claimsExtractor = claimsExtractor;
        this.claimsUserFactory = claimsUserFactory;
        this.authoritiesResolver = authoritiesResolver;
    }

    @Override
    public UserDetails apply(String headerValue) {

        Claims claims = claimsExtractor.apply(headerValue);

        User user = claimsUserFactory.apply(claims);

        Collection<? extends GrantedAuthority> authorities = Collections.emptyList();
        if (authoritiesResolver != null) {
            authorities = authoritiesResolver.apply(claims);
        }

// Path: core/src/main/java/org/juiser/model/User.java
// public interface User {
// 
//     /**
//      * Returns {@code true} if the user has been authenticated (proven their identity) or {@code false} if the user
//      * is considered anonymous.
//      *
//      * @return {@code true} if the user has been authenticated (proven their identity) or {@code false} if the user
//      * is considered anonymous.
//      */
//     boolean isAuthenticated();
// 
//     /**
//      * Returns {@code true} if the user identity is unknown, {@code false} if the user is authenticated.
//      *
//      * @return {@code true} if the user identity is unknown, {@code false} if the user is authenticated.
//      */
//     boolean isAnonymous();
// 
//     String getHref();
// 
//     String getId();
// 
//     String getName(); //full name
// 
//     String getGivenName(); //aka 'first name' in Western cultures
// 
//     String getFamilyName(); //aka surname
// 
//     String getMiddleName();
// 
//     String getNickname(); //aka 'Mike' if givenName is 'Michael'
// 
//     String getUsername(); //OIDC 'preferred_username'
// 
//     URL getProfile();
// 
//     URL getPicture();
// 
//     URL getWebsite();
// 
//     String getEmail();
// 
//     boolean isEmailVerified();
// 
//     String getGender();
// 
//     LocalDate getBirthdate();
// 
//     TimeZone getZoneInfo();
// 
//     Locale getLocale();
// 
//     Phone getPhone();
// 
//     String getPhoneNumber();
// 
//     boolean isPhoneNumberVerified();
// 
//     ZonedDateTime getCreatedAt();
// 
//     ZonedDateTime getUpdatedAt();
// }
// 
// Path: spring/spring-security/src/main/java/org/juiser/spring/security/core/userdetails/ForwardedUserDetails.java
// public class ForwardedUserDetails implements UserDetails {
// 
//     private final User user;
//     private final Collection<? extends GrantedAuthority> grantedAuthorities;
// 
//     public ForwardedUserDetails(User user, Collection<? extends GrantedAuthority> grantedAuthorities) {
//         Assert.notNull(user, "User argument cannot be null.");
//         Assert.hasText(user.getUsername(), "User username cannot be null or empty.");
//         this.user = user;
//         if (CollectionUtils.isEmpty(grantedAuthorities)) {
//             this.grantedAuthorities = java.util.Collections.emptyList();
//         } else {
//             this.grantedAuthorities = java.util.Collections.unmodifiableCollection(grantedAuthorities);
//         }
//     }
// 
//     public User getUser() {
//         return this.user;
//     }
// 
//     @Override
//     public Collection<? extends GrantedAuthority> getAuthorities() {
//         return this.grantedAuthorities;
//     }
// 
//     @Override
//     public String getPassword() {
//         return null;
//     }
// 
//     @Override
//     public String getUsername() {
//         return getUser().getUsername();
//     }
// 
//     @Override
//     public boolean isAccountNonExpired() {
//         return true; //gateway will not forward expired accounts
//     }
// 
//     @Override
//     public boolean isAccountNonLocked() {
//         return true; //gateway will not forward locked accounts
//     }
// 
//     @Override
//     public boolean isCredentialsNonExpired() {
//         return true; //gateway will not forward expired accounts
//     }
// 
//     @Override
//     public boolean isEnabled() {
//         return true; //gateway will not forward disabled accounts
//     }
// }


// Path: spring/spring-security/src/main/java/org/juiser/spring/security/authentication/JwsToUserDetailsConverter.java
import org.juiser.model.User;
import org.juiser.spring.security.core.userdetails.ForwardedUserDetails;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.lang.Assert;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Function;

/*
 * Copyright 2017 Les Hazlewood and the respective Juiser contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.juiser.spring.security.authentication;



/**
 * @since 1.0.0
 */
public class JwsToUserDetailsConverter implements Function<String, UserDetails> {

    private final Function<String, Claims> claimsExtractor;
    private final Function<Claims, User> claimsUserFactory;
    private final Function<Claims, Collection<? extends GrantedAuthority>> authoritiesResolver;

    public JwsToUserDetailsConverter(Function<String, Claims> claimsExtractor,
                                     Function<Claims, User> claimsUserFactory,
                                     Function<Claims, Collection<? extends GrantedAuthority>> authoritiesResolver) {
        Assert.notNull(claimsExtractor, "claimsExtractor cannot be null.");
        Assert.notNull(claimsUserFactory, "claimsUserFactory cannot be null.");
        this.claimsExtractor = claimsExtractor;
        this.claimsUserFactory = claimsUserFactory;
        this.authoritiesResolver = authoritiesResolver;
    }

    @Override
    public UserDetails apply(String headerValue) {

        Claims claims = claimsExtractor.apply(headerValue);

        User user = claimsUserFactory.apply(claims);

        Collection<? extends GrantedAuthority> authorities = Collections.emptyList();
        if (authoritiesResolver != null) {
            authorities = authoritiesResolver.apply(claims);
        }

        return new ForwardedUserDetails(user, authorities);