001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Arnaud Kervern 018 */ 019package org.nuxeo.ecm.platform.oauth2.clients; 020 021import static org.nuxeo.ecm.platform.oauth2.clients.ClientRegistry.OAUTH2CLIENT_SCHEMA; 022 023import java.util.HashMap; 024import java.util.Map; 025 026import org.apache.commons.lang.StringUtils; 027import org.nuxeo.common.xmap.annotation.XNode; 028import org.nuxeo.common.xmap.annotation.XObject; 029import org.nuxeo.ecm.core.api.DocumentModel; 030 031/** 032 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a> 033 * @since 5.9.2 034 */ 035@XObject("client") 036public class OAuth2Client { 037 038 @XNode("@name") 039 protected String name; 040 041 @XNode("@id") 042 protected String id; 043 044 @XNode("@secret") 045 protected String secret; 046 047 @XNode("@enabled") 048 protected boolean enabled = true; 049 050 public OAuth2Client() { 051 } 052 053 public OAuth2Client(String name, String id, String secret) { 054 this.name = name; 055 this.id = id; 056 this.secret = secret; 057 } 058 059 public String getId() { 060 return id; 061 } 062 063 public void setId(String id) { 064 this.id = id; 065 } 066 067 public String getSecret() { 068 return secret; 069 } 070 071 public void setSecret(String secret) { 072 this.secret = secret; 073 } 074 075 public String getName() { 076 return name; 077 } 078 079 public void setName(String name) { 080 this.name = name; 081 } 082 083 public boolean isEnabled() { 084 return enabled; 085 } 086 087 public void setEnabled(boolean enable) { 088 this.enabled = enable; 089 } 090 091 Map<String, Object> toMap() { 092 Map<String, Object> doc = new HashMap<>(); 093 doc.put("clientId", id); 094 doc.put("clientSecret", secret); 095 doc.put("name", name); 096 doc.put("enabled", enabled); 097 return doc; 098 } 099 100 static OAuth2Client fromDocumentModel(DocumentModel doc) { 101 String name = (String) doc.getPropertyValue(OAUTH2CLIENT_SCHEMA + ":name"); 102 String id = (String) doc.getPropertyValue(OAUTH2CLIENT_SCHEMA + ":clientId"); 103 String secret = (String) doc.getPropertyValue(OAUTH2CLIENT_SCHEMA + ":clientSecret"); 104 boolean enabled = (Boolean) doc.getPropertyValue(OAUTH2CLIENT_SCHEMA + ":enabled"); 105 106 OAuth2Client client = new OAuth2Client(name, id, secret); 107 client.enabled = enabled; 108 return client; 109 } 110 111 boolean isValidWith(String clientId, String clientSecret) { 112 // Related to RFC 6749 2.3.1 clientSecret is omitted if empty 113 return enabled && id.equals(clientId) && (StringUtils.isEmpty(secret) || secret.equals(clientSecret)); 114 } 115}