001package ca.cdr.api.model.enm; 002 003/* 004 * #%L 005 * Smile CDR - CDR 006 * %% 007 * Copyright (C) 2016 - 2025 Smile CDR, Inc. 008 * %% 009 * All rights reserved. 010 * #L% 011 */ 012 013import org.apache.commons.lang3.Validate; 014 015import java.util.Arrays; 016import java.util.Map; 017import java.util.stream.Collectors; 018 019public enum OAuth2AuthorizedGrantTypeEnum { 020 021 /* 022 * Sorting OK, but don't rename these! 023 */ 024 025 AUTHORIZATION_CODE("authorization_code"), 026 027 IMPLICIT("implicit"), 028 029 REFRESH_TOKEN("refresh_token"), 030 031 CLIENT_CREDENTIALS("client_credentials"), 032 033 PASSWORD("password"), 034 035 JWT_BEARER("urn:ietf:params:oauth:grant-type:jwt-bearer"), 036 ; 037 038 private static Map<String, OAuth2AuthorizedGrantTypeEnum> ourCodes; 039 private String myCode; 040 041 /** 042 * Constructor 043 */ 044 OAuth2AuthorizedGrantTypeEnum(String theCode) { 045 myCode = theCode; 046 } 047 048 public String getCode() { 049 return myCode; 050 } 051 052 public static synchronized boolean hasCode(String theGrantType) { 053 return getCodeToEnums().containsKey(theGrantType); 054 } 055 056 private static Map<String, OAuth2AuthorizedGrantTypeEnum> getCodeToEnums() { 057 Map<String, OAuth2AuthorizedGrantTypeEnum> codes = ourCodes; 058 if (codes == null) { 059 codes = Arrays.stream(OAuth2AuthorizedGrantTypeEnum.values()) 060 .collect(Collectors.toMap(t -> t.getCode(), t -> t)); 061 ourCodes = codes; 062 } 063 return codes; 064 } 065 066 public static OAuth2AuthorizedGrantTypeEnum forCode(String theGrantType) { 067 OAuth2AuthorizedGrantTypeEnum retVal = getCodeToEnums().get(theGrantType); 068 Validate.notNull(retVal, "Invalid grant type: %s", theGrantType); 069 return retVal; 070 } 071}