OAuth 2.0 Scopes and Fine-Grained Permissions
OAuth 2.0 Scopes and Fine-Grained Permissions
OAuth 2.0 scopes provide a standardized mechanism for delegated authorization. Scopes define the specific permissions a client application requests and a user grants. Well-designed scope systems enable users to understand and control what access they're granting while giving applications the permissions they need to function.
// Java Spring Security example of OAuth 2.0 scope-based authorization
@RestController
@RequestMapping("/api")
public class DataController {
@GetMapping("/profile")
@PreAuthorize("hasAuthority('SCOPE_profile:read')")
public ResponseEntity<UserProfile> getProfile(Authentication auth) {
String userId = auth.getName();
UserProfile profile = userService.getProfile(userId);
return ResponseEntity.ok(profile);
}
@PutMapping("/profile")
@PreAuthorize("hasAuthority('SCOPE_profile:write')")
public ResponseEntity<UserProfile> updateProfile(
@RequestBody UserProfile profile,
Authentication auth) {
String userId = auth.getName();
UserProfile updated = userService.updateProfile(userId, profile);
return ResponseEntity.ok(updated);
}
@DeleteMapping("/account")
@PreAuthorize("hasAuthority('SCOPE_account:delete')")
public ResponseEntity<Void> deleteAccount(Authentication auth) {
String userId = auth.getName();
userService.deleteAccount(userId);
return ResponseEntity.noContent().build();
}
}
Scope design requires balancing granularity with usability. Overly broad scopes like "full_access" provide poor security isolation, while excessively granular scopes create user confusion and consent fatigue. Design scopes that map to logical user actions and group related permissions. Use hierarchical scopes where broader scopes include narrower ones.
Dynamic scope evaluation enhances OAuth 2.0's authorization capabilities. Instead of static scope checks, evaluate scopes in context with additional factors. For instance, a "transactions:read" scope might allow reading all transactions for admin users but only own transactions for regular users. This approach combines OAuth 2.0's standardization with flexible authorization logic.