001package org.nuxeo.ecm.platform.oauth2.clients; 002 003import static org.nuxeo.ecm.platform.oauth2.clients.ClientRegistry.OAUTH2CLIENT_SCHEMA; 004 005import java.util.HashMap; 006import java.util.Map; 007 008import org.apache.commons.lang.StringUtils; 009import org.nuxeo.common.xmap.annotation.XNode; 010import org.nuxeo.common.xmap.annotation.XObject; 011import org.nuxeo.ecm.core.api.DocumentModel; 012 013/** 014 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a> 015 * @since 5.9.2 016 */ 017@XObject("client") 018public class OAuth2Client { 019 020 @XNode("@name") 021 protected String name; 022 023 @XNode("@id") 024 protected String id; 025 026 @XNode("@secret") 027 protected String secret; 028 029 @XNode("@enabled") 030 protected boolean enabled = true; 031 032 public OAuth2Client() { 033 } 034 035 public OAuth2Client(String name, String id, String secret) { 036 this.name = name; 037 this.id = id; 038 this.secret = secret; 039 } 040 041 public String getId() { 042 return id; 043 } 044 045 public void setId(String id) { 046 this.id = id; 047 } 048 049 public String getSecret() { 050 return secret; 051 } 052 053 public void setSecret(String secret) { 054 this.secret = secret; 055 } 056 057 public String getName() { 058 return name; 059 } 060 061 public void setName(String name) { 062 this.name = name; 063 } 064 065 public boolean isEnabled() { 066 return enabled; 067 } 068 069 public void setEnabled(boolean enable) { 070 this.enabled = enable; 071 } 072 073 Map<String, Object> toMap() { 074 Map<String, Object> doc = new HashMap<>(); 075 doc.put("clientId", id); 076 doc.put("clientSecret", secret); 077 doc.put("name", name); 078 doc.put("enabled", enabled); 079 return doc; 080 } 081 082 static OAuth2Client fromDocumentModel(DocumentModel doc) { 083 String name = (String) doc.getPropertyValue(OAUTH2CLIENT_SCHEMA + ":name"); 084 String id = (String) doc.getPropertyValue(OAUTH2CLIENT_SCHEMA + ":clientId"); 085 String secret = (String) doc.getPropertyValue(OAUTH2CLIENT_SCHEMA + ":clientSecret"); 086 boolean enabled = (Boolean) doc.getPropertyValue(OAUTH2CLIENT_SCHEMA + ":enabled"); 087 088 OAuth2Client client = new OAuth2Client(name, id, secret); 089 client.enabled = enabled; 090 return client; 091 } 092 093 boolean isValidWith(String clientId, String clientSecret) { 094 // Related to RFC 6749 2.3.1 clientSecret is omitted if empty 095 return enabled && id.equals(clientId) && (StringUtils.isEmpty(secret) || secret.equals(clientSecret)); 096 } 097}