001 package com.nimbusds.oauth2.sdk; 002 003 004 import java.util.HashSet; 005 006 import net.jcip.annotations.NotThreadSafe; 007 008 009 /** 010 * Authorisation scope. This class is not thread-safe. 011 * 012 * <p>Example scope from OpenID Connect indicating access to the user's email 013 * and profile details: 014 * 015 * <pre> 016 * Scope scope = new Scope(); 017 * scope.add(OIDCScopeToken.OPENID); 018 * scope.add(OIDCScopeToken.EMAIL); 019 * scope.add(OIDCScopeToken.PROFILE); 020 * </pre> 021 * 022 * <p>Related specifications: 023 * 024 * <ul> 025 * <li>OAuth 2.0 (RFC 6749), section 3.3. 026 * </ul> 027 * 028 * @author Vladimir Dzhuvinov 029 * @version $version$ (2013-01-19) 030 */ 031 @NotThreadSafe 032 public class Scope extends HashSet<ScopeToken> { 033 034 035 /** 036 * Creates a new empty authorisation scope. 037 */ 038 public Scope() { 039 040 // Nothing to do 041 } 042 043 044 /** 045 * Returns the string representation of this scope. The scope tokens 046 * can be serialised in any order. 047 * 048 * @return The string representation. 049 */ 050 @Override 051 public String toString() { 052 053 StringBuilder sb = new StringBuilder(); 054 055 for (ScopeToken token: this) { 056 057 if (sb.length() > 0) 058 sb.append(' '); 059 060 sb.append(token.toString()); 061 } 062 063 return sb.toString(); 064 } 065 066 067 /** 068 * Parses a scope from the specified string representation. 069 * 070 * @param s The scope string, {@code null} if not specified. 071 * 072 * @return The scope, {@code null} if not specified. 073 */ 074 public static Scope parse(final String s) { 075 076 if (s == null) 077 return null; 078 079 Scope scope = new Scope(); 080 081 if (s.trim().isEmpty()) 082 return scope; 083 084 String[] tokens = s.split("\\s+"); 085 086 for (String t: tokens) 087 scope.add(new ScopeToken(t)); 088 089 return scope; 090 } 091 }