github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/model/iyo/iyo.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package oauthiyo
     5  
     6  import (
     7  	"encoding/json"
     8  	"io"
     9  	"strings"
    10  
    11  	"github.com/mattermost/mattermost-server/einterfaces"
    12  	"github.com/mattermost/mattermost-server/model"
    13  	"fmt"
    14  )
    15  
    16  type IyoProvider struct {
    17  }
    18  
    19  type IyoEmailAddress struct {
    20  	EmailAddress   string  `json:"emailaddress"`
    21  }
    22  
    23  type IyoUser struct {
    24  	UserName       string 				`json:"username"`
    25  	FirstName      string 				`json:"firstname"`
    26  	LastName       string 				`json:"lastname"`
    27  	EmailAddresses []IyoEmailAddress 	`json:"emailaddresses"`
    28  }
    29  
    30  func init() {
    31  	provider := &IyoProvider{}
    32  	einterfaces.RegisterOauthProvider(model.USER_AUTH_SERVICE_IYO, provider)
    33  }
    34  
    35  func userFromIyoUser(iyoUser *IyoUser) *model.User {
    36  	user := &model.User{}
    37  	if len(iyoUser.EmailAddresses) > 0 {
    38  		user.Email = iyoUser.EmailAddresses[0].EmailAddress
    39  		user.Username = strings.Split(user.Email, "@")[0]
    40  	}
    41  	user.FirstName = iyoUser.FirstName
    42  	user.LastName = iyoUser.LastName
    43  	user.AuthData = &iyoUser.UserName
    44  	user.AuthService = model.USER_AUTH_SERVICE_IYO
    45  	fmt.Printf("%#v", user)
    46  	return user
    47  }
    48  
    49  func iyoUserFromJson(data io.Reader) *IyoUser {
    50  	decoder := json.NewDecoder(data)
    51  	var iyoUser IyoUser
    52  	err := decoder.Decode(&iyoUser)
    53  	fmt.Printf("%#v", iyoUser)
    54  	if err == nil {
    55  		return &iyoUser
    56  	} else {
    57  		return nil
    58  	}
    59  }
    60  
    61  func (iyoUser *IyoUser) ToJson() string {
    62  	b, err := json.Marshal(iyoUser)
    63  	if err != nil {
    64  		return ""
    65  	} else {
    66  		return string(b)
    67  	}
    68  }
    69  
    70  func (iyoUser *IyoUser) IsValid() bool {
    71  	if len(iyoUser.UserName) == 0 {
    72  		return false
    73  	}
    74  
    75  	if len(iyoUser.EmailAddresses) == 0 {
    76  		return false
    77  	}
    78  
    79  	return true
    80  }
    81  
    82  func (iyoUser *IyoUser) getAuthData() string {
    83  	return iyoUser.UserName
    84  }
    85  
    86  func (m *IyoProvider) GetIdentifier() string {
    87  	return model.USER_AUTH_SERVICE_IYO
    88  }
    89  
    90  func (m *IyoProvider) GetUserFromJson(data io.Reader) *model.User {
    91  	iyoUser := iyoUserFromJson(data)
    92  	if iyoUser.IsValid() {
    93  		return userFromIyoUser(iyoUser)
    94  	}
    95  
    96  	return &model.User{}
    97  }
    98  
    99  func (m *IyoProvider) GetAuthDataFromJson(data io.Reader) string {
   100  	iyoUser := iyoUserFromJson(data)
   101  	if iyoUser.IsValid() {
   102  		return iyoUser.getAuthData()
   103  	}
   104  
   105  	return ""
   106  }